package db;
import java.sql.*;
public class DataBaseConnection {
Connection con = null;
// 数据库连接方法
public Connection getConn() {
try {
Class.forName("com.mysql.jdbc.Driver");
//sql server改为:
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con = DriverManager.getConnection("jdbc:mysql://localhost/kb_test? useUnicode=true&characterEncoding=utf8",
"root", "123456");
//sql server代码为:
//con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=数据库名字","账号", "密码");
//Oracle 改为:
connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:默认数据库名", "账号", "密码");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
// 数据库连接方法
public Connection getConn_104() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con = DriverManager.getConnection(
"jdbc:mysql://192.68.68.104/kb_test?useUnicode=true&characterEncoding=utf8", "test", "test");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
// 数据库关闭方法
public void close() {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import dao.UserDao;
import db.DataBaseConnection;
import entity.User;
public class UserDaoImpl implements UserDao {
public void insert(User user) {
// TODO Auto-generated method stub
//数据库连接
DataBaseConnection dbc = new DataBaseConnection();
Connection con=dbc.getConn();
PreparedStatement pstmt=null;
//执行sql语句
String sql ="insert into customerinfo (id,password,name,sex,address,code) values(?,?,?,?,?,?)";
try {
pstmt = con.prepareStatement(sql) ; //发送sql语句,删除书籍基本信息
pstmt.setString(1,user.getId()) ;
pstmt.setString(2,user.getPassword()) ;
pstmt.setString(3,user.getName()) ;
pstmt.setString(4,user.getSex()) ;
pstmt.setString(5,user.getAddress()) ;
pstmt.setString(6,user.getCode()) ;
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void updata(User user) {
// TODO Auto-generated method stub
String sql = "UPDATE customerinfo SET id=?,password=?,name=?,sex=?,address=?,code=? WHERE id=?" ;
DataBaseConnection dbc = new DataBaseConnection();
Connection con=dbc.getConn();
PreparedStatement pstmt=null;
try
{
pstmt = dbc.getConn().prepareStatement(sql) ;
pstmt.setString(1,user.getId());
pstmt.setString(2,user.getPassword());
pstmt.setString(3,user.getName()) ;
pstmt.setString(4,user.getSex());
pstmt.setString(5,user.getAddress());
pstmt.setString(6,user.getCode()) ;
pstmt.setString(7,user.getId()) ;
pstmt.executeUpdate() ;
pstmt.close() ;
}
catch (Exception e)
{
try {
//throw new Exception("操作中出现错误!!!") ;
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
finally
{
dbc.close() ;
}
}
public void delete(String id) {
// TODO Auto-generated method stub
String sql = "DELETE FROM customerinfo WHERE id=?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = new DataBaseConnection() ;
try
{
pstmt = dbc.getConn().prepareStatement(sql) ;
pstmt.setString(1,id) ;
pstmt.executeUpdate() ;
pstmt.close() ;
}
catch (Exception e)
{
try {
throw new Exception("操作中出现错误!!!") ;
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
finally
{
dbc.close() ;
}
}
public User queryById(String id) {
// TODO Auto-generated method stub
User user = new User() ;
String sql = "SELECT * FROM customerinfo WHERE id=?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc =new DataBaseConnection() ;
try
{
pstmt = dbc.getConn().prepareStatement(sql) ;
pstmt.setString(1,id) ;
ResultSet rs = pstmt.executeQuery() ;
if (rs.next()){
user.setId(rs.getString("id")) ;
user.setPassword(rs.getString("password"));
user.setName(rs.getString("name"));
user.setSex(rs.getString("sex"));
user.setAddress(rs.getString("address"));
user.setCode(rs.getString("code"));
user.setIndex(Integer.parseInt(rs.getString("index")));
}
rs.close() ;
pstmt.close() ;
}
catch (Exception e)
{
System.out.print(e);
e.printStackTrace();
}
finally
{
dbc.close() ;
}
return user ;
}
public List query() {
// TODO Auto-generated method stub
List all = new ArrayList() ;
//连接数据库
DataBaseConnection dbc = new DataBaseConnection();
Connection con=dbc.getConn();
//执行sql语句
String sql = "SELECT * FROM customerinfo" ;
PreparedStatement pstmt;
try
{
pstmt = con.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next())
{
User user= new User();
user.setId(rs.getString("id")) ;
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setSex(rs.getString("sex"));
user.setAddress(rs.getString("address"));
user.setCode(rs.getString("code"));
user.setIndex(Integer.parseInt(rs.getString("index")));
all.add(user) ;
}
rs.close() ;
pstmt.close() ;
con.close();
}
catch (Exception e)
{
System.out.println(e) ;
try {
throw new Exception("操作中出现错误!!!") ;
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
finally
{
dbc.close() ;
}
return all ;
}
public void PLdelete(String[] id) {
// TODO Auto-generated method stub
//连接数据库
DataBaseConnection dbc = new DataBaseConnection();
Connection con=dbc.getConn();
for(int i=0;i
未添加链接驱动
报错原因是数据库驱动版本问题,数据库连接驱动和对应框架的版本不同,应该更改驱动jar的版本
原因:Sting类型没有加单引号
原因,主键值存在或混乱,要先删除数据或者清空表
原因:可能输入的值的类型不对,确定是否插入的元素时对应的值的类型正确
原因:数据过长导致被截断,注意对应值的类型与长度限制——一般把长度调高一点就可以了
private static String dbUrl="jdbc:mysql://localhost:3306/db_book2?useUnicode=true&characterEncoding=utf-8";
String str="C:\\152.txt";
String location=str.replace("\\\\", "/");
File context=new File(location);