c3p0连接数据库,数据池查询,插入数据

1.导入c3p0的jar包

 1 package cn.tedu.dao;
 2 
 3 import java.sql.ResultSet;
 4 
 5 import java.sql.Connection;
 6 import java.sql.PreparedStatement;
 7 import java.sql.ResultSet;
 8 import java.sql.SQLException;
 9 import java.sql.Statement;
10 
11 import org.apache.catalina.connector.Request;
12 import org.apache.commons.codec.digest.DigestUtils;
13 import org.eclipse.jdt.internal.compiler.batch.Main;
14 
15 import cn.tedu.bean.User;
16 import cn.tedu.service.UserService;
17 import cn.tedu.util.C3P0Util;
18 
19 public class UserDao {
20     /*
21      * 根据用户名查询
22      * 如果用户名存在,封装user,返回
23      * */
24         public User selectUserByName(String name){
25         User user=null;
26         Connection conn = C3P0Util.getConn();
27         String sql="select * from tb_user where name=?";
28         PreparedStatement pstmt;
29         try{
30             pstmt=conn.prepareStatement(sql);
31             pstmt.setString(1, name);
32             ResultSet rs=pstmt.executeQuery();
33             if(rs.next()){
34                 user =new User();
35                 user.setId(rs.getInt(1));
36                 user.setName(rs.getString(2));
37                 user.setPassword(rs.getString(3));
38                 user.setEmail(rs.getString(4));
39                 user.setPhone(rs.getString(5));
40                 user.setImage(rs.getString(6));
41                 user.setGender(rs.getString(7));
42                 
43             }
44         }catch(Exception e){
45             e.printStackTrace();
46         }
47         return user;
48         }
49         public void insertUser(User user){
50             /*
51              * 插入用户
52              * */
53             Connection conn = C3P0Util.getConn();
54             String sql="insert into tb_user(name,password,email,phone,image,gender) values(?,?,?,?,?,?)";
55             PreparedStatement pstmt;
56             try {
57                 //System.out.println("zoule");
58                 pstmt = conn.prepareStatement(sql);
59                 pstmt.setString(1,user.getName());
60                 pstmt.setString(2, user.getPassword());
61                 pstmt.setString(3, user.getEmail());
62                 pstmt.setString(4, user.getPhone());
63                 pstmt.setString(5, user.getImage());
64                 pstmt.setString(6, user.getGender());
65                 pstmt.executeUpdate();
66                 
67             } catch (SQLException e) {
68                 // TODO Auto-generated catch block
69                 e.printStackTrace();
70             }
71         }
72         public static void main(String[] args) {
73             /*UserDao userDao=new UserDao();
74             System.out.println(userDao.selectUserByName("admin"));*/
75         
76             User user=new User();
77             user.setName("admin2");
78             user.setPassword("123456");
79             user.setEmail("teduin@22.cn");
80             user.setPhone("11215151");
81             user.setImage("konjgn");
82             user.setGender("efaff");
83             new UserDao().insertUser(user);
84             /*User user=new User();
85             System.out.println(DigestUtils.md5Hex("123"));
86             new UserService().addUser(user);*/
87         }
88 }

 

2.在c3p0.xml配置账号密码,数据库

package cn.tedu.dao;

import java.sql.ResultSet;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.catalina.connector.Request;
import org.apache.commons.codec.digest.DigestUtils;
import org.eclipse.jdt.internal.compiler.batch.Main;

import cn.tedu.bean.User;
import cn.tedu.service.UserService;
import cn.tedu.util.C3P0Util;

public class UserDao {
/*
* 根据用户名查询
* 如果用户名存在,封装user,返回
* */
public User selectUserByName(String name){
User user=null;
Connection conn = C3P0Util.getConn();
String sql="select * from tb_user where name=?";
PreparedStatement pstmt;
try{
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, name);
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
user =new User();
user.setId(rs.getInt(1));
user.setName(rs.getString(2));
user.setPassword(rs.getString(3));
user.setEmail(rs.getString(4));
user.setPhone(rs.getString(5));
user.setImage(rs.getString(6));
user.setGender(rs.getString(7));

}
}catch(Exception e){
e.printStackTrace();
}
return user;
}
public void insertUser(User user){
/*
* 插入用户
* */
Connection conn = C3P0Util.getConn();
String sql="insert into tb_user(name,password,email,phone,image,gender) values(?,?,?,?,?,?)";
PreparedStatement pstmt;
try {
//System.out.println("zoule");
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,user.getName());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getEmail());
pstmt.setString(4, user.getPhone());
pstmt.setString(5, user.getImage());
pstmt.setString(6, user.getGender());
pstmt.executeUpdate();

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
/*UserDao userDao=new UserDao();
System.out.println(userDao.selectUserByName("admin"));*/

User user=new User();
user.setName("admin2");
user.setPassword("123456");
user.setEmail("teduin@22.cn");
user.setPhone("11215151");
user.setImage("konjgn");
user.setGender("efaff");
new UserDao().insertUser(user);
/*User user=new User();
System.out.println(DigestUtils.md5Hex("123"));
new UserService().addUser(user);*/
}
}

posted @ 2020-08-28 10:27  来咯相信我  阅读(537)  评论(0编辑  收藏  举报