码迷,mamicode.com
首页 > 数据库 > 详细

MySQL第五天

时间:2019-04-08 21:17:31      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:net   trace   添加   表名   roo   lis   执行sql   获得   connect   

  C3P0连接池配置文件

public static void main(String[] args) throws Exception {
//从控制台输入用户名和密码
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名");
String name = sc.nextLine();
System.out.println("请输入密码");
String password = sc.nextLine();
login(name,password);
}
public static void login(String name,String password) throws Exception{
Connection con = JDBCUtils.getConnection();
//1.编写sql 未知内容使用?占位符
String sql = "select*from yang where ename=? and mgr=?";
//2.获得PreparedStatemnet对象
PreparedStatement ps = con.prepareStatement(sql);
//3.获得实际的参数
ps.setString(1,name);
ps.setString(2,password);
//4.执行sql语句
ResultSet rs = ps.executeQuery();
if (rs.next()){
System.out.println("登陆成功");
}else {
System.out.println("登陆失败");
}
//5.关闭资源
JDBCUtils.close(con,ps, rs);
}
Druid连接池配置文件
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///girl
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000
工具类
public class JDBCUtils {
//1.定义成员变量
private static DataSource ds;
static {
//2.加载配置文件 获得连接池
try {
Properties pro = new Properties();
pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
ds= DruidDataSourceFactory.createDataSource(pro);
}catch (Exception e){
e.printStackTrace();
}
}
//3.获得链接
public static Connection getConnection() throws Exception{
return ds.getConnection();
}
//4.释放资源
public static void close(Connection con, Statement stat){
if (stat!=null){
try {
stat.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (con!=null){
try {
con.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
public static void close(Connection con, Statement stat, ResultSet rs){
if (rs!=null){
try {
rs.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (stat!=null){
try {
stat.close();
}catch (Exception e){
e.printStackTrace();
}
}
if (con!=null){
try {
con.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
//4.获得链接池
public static DataSource getDataSource(){
return ds;
}
}
Spring连接池
public static void main(String[] args) {
//1 导包
//2 创建JDBCTemplate 对象 依赖于DataSource
JdbcTemplate jt = new JdbcTemplate(JDBCUtils.getDataSource());
String sql = "update yang set salary=2019 where id=?";
int i = jt.update(sql,1001);
System.out.println(i);
}
封装一个类,类名与已存的某个表名一致,对表的内容进行操作
public class TempleteDemo3 {
JdbcTemplate jt =new JdbcTemplate(JDBCUtils.getDataSource());
//1,修改
@Test
public void test1(){
String sql ="update yang set salary = 1999 where id=?";
int i = jt.update(sql,1001);
System.out.println(i);
}
//2添加一条记录
@Test
public void test2(){
String sql = "insert into yang (id,ename,salary) values(?,?,?)";
int i = jt.update(sql,1015,"杨蒙蒙","12001");
System.out.println(i);
}
//3删除一条记录
@Test
public void test3(){
String sql = "delete from yang where id=?";
int i = jt.update(sql,1015);
System.out.println(i);
}
//4.查询id为1001的记录,将其封装为map集合
@Test
public void test4(){
String sql ="select *from yang where id=?";
Map<String,Object> map = jt.queryForMap(sql,1001);
System.out.println(map);
}
//5.查询所有记录,将其封装为List集合
@Test

public void test5(){
String sql = "select*from yang";
List<Map<String,Object>> list = jt.queryForList(sql);
for (Map<String,Object> map :list){
System.out.println(map);
}
}
//6.查询所有记录,将其封装为yang对象的List集合
@Test
public void test6(){
String sql = "select*from yang";
List<yang> list = jt.query(sql,new BeanPropertyRowMapper<yang>(yang.class));
for (yang y:list){
System.out.println(y);
}
}
//7.查询总记录数
@Test
public void test7(){
String sql = "select count(id) from yang";
Long l = jt.queryForObject(sql,Long.class);
System.out.println(l);
}


 
 
 

MySQL第五天

标签:net   trace   添加   表名   roo   lis   执行sql   获得   connect   

原文地址:https://www.cnblogs.com/Y-mmeng/p/10673203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!