获得数据库的链接
加载mysql驱动:Class.forName("com.mysql.jdbc.Driver");
加载oracle驱动:Class.forName("oracle.jdbc.driver.OracleDriver");
加载相应的驱动需要导入相应的包,如MySQL则需要导入:mysql-connector-java-5.1.13-bin.jar
否则无法正常执行。
Connetion类主要用来链接数据库,常通过DriverManager.getConnection()来获取一个连接对象。
Statement对象用于执行sql语句,有以下3种:
(1)Statement对象用于执行不带参数的简单的SQL语句;
(2)PerparedStatement对象用于执行带或不带参数的预编译SQL语句;
(3)CallableStatement对象用于执行对数据库已存储过程的调用;
Statement的常用方法:
(1)executeQuery()方法:运行查询语句,返回ReaultSet对象。
(2)executeUpdata()方法:运行增,删,改操作,返回更新的行数。
(3)addBatch(String sql) :把多条sql语句放到一个批处理中。
(4)executeBatch():向数据库发送一批sql语句执行
执行executeQuery()方法后返回的结果集常用方法:
(1)getString(String columnName):获得当前行的某一string类型的字段
(2)getFloat(String columnName):获得当前行的某一string类型的字段
(3)getDate(String columnName):获得当前行的某一date类型的字段
(4)getBoolean(String columnName):获得在当前行的某一Boolean类型的字段
(5)getObject(String columnName):获取当前行的某一任意类型的字段
(6)next():移动到下一行
public Connection getCon() throws Exception{
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 获得数据库链接
String url = "jdbc:mysql://localhost:3306/####"; //数据库 链接地址
String username="root"; //数据库的帐号
String password="root"; //数据库的密码
Connection connection= DriverManager.getConnection(url,username,password);
return connection;
}
关闭资源
public void closeResource(ResultSet resultSet,PreparedStatement preparedStatement,
Connection connection){
try {
if(resultSet!=null){
resultSet.close();
}
if(preparedStatement!=null){
preparedStatement.close();
}
if(connection!=null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
增加
public void addXXX(){
Connection connection=null; //数据库链接
PreparedStatement preparedStatement=null; //预编译对象
try {
connection=this.getCon(); //获得数据库链接
//编写sql语句 ?占位符 给?赋值
String sql="INSERT INTO news_detail(categoryId,title,summary,content," +
"picPath,author,createDate) VALUES(?,?,?,?,?,?,?)";
//预编译sql语句
preparedStatement=connection.prepareStatement(sql);
//给占位符(?) 赋值
preparedStatement.setInt(1,1);
preparedStatement.setString(2,"测试标题");
preparedStatement.setString(3,"测试摘要");
preparedStatement.setString(4,"测试内容");
preparedStatement.setString(5,"");
preparedStatement.setString(6,"");
Date nowTime=new Date(); //当前时间
long time= nowTime.getTime();//当前时间到1970年1月1日对应的毫秒数
Timestamp timestamp=new Timestamp(time);//根据上面的毫秒数,获得当前时间对应的时间戳
preparedStatement.setTimestamp(7,timestamp);//设置创建新闻的时间为当前时间
preparedStatement.executeUpdate(); //执行新增操作
} catch (Exception e) {
e.printStackTrace();
}finally {
//关闭资源
closeResource(null,preparedStatement,connection);
}
}
删除
public void deleteXXX(){
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
//1 获得数据库链接
connection= this.getCon();
//2 编写sql语句 预编译
String sql="DELETE FROM news_detail WHERE id=?";
preparedStatement=connection.prepareStatement(sql);
//3 给sql语句传递参数
preparedStatement.setInt(1,3);
//4 执行sql语句
preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
closeResource(null,preparedStatement,connection); //5 关闭资源
}
}
修改
public void updateXXX(){
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
connection=this.getCon();//获得数据库链接
String sql="UPDATE news_detail SET title=? WHERE id=?";//编写sql语句
preparedStatement=connection.prepareStatement(sql);//预编译sql语句
//给sql语句占位符赋值
preparedStatement.setString(1,"KGCJAVA");
preparedStatement.setInt(2,2);
//执行sql语句
preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
closeResource(null,preparedStatement,connection); //关闭资源
}
}
查询
public void XXXQuery(){ //查询
Connection connection=null; //数据库链接
PreparedStatement preparedStatement=null; //预编译对象
ResultSet resultSet=null; //结果集
try {
connection= getCon(); //1 获得数据库的链接
String sql="select * from news_detail"; //2 编写sql语句 预编译sql语句
preparedStatement= connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery(); //3 执行sql语句
//4 遍历结果集
while(resultSet.next()){ //判断结果集中是否有下一条记录,如果有获得该条记录
int id= resultSet.getInt("id");
int categoryId=resultSet.getInt("categoryId");
String title= resultSet.getString("title");
String summary= resultSet.getString("summary");
String content= resultSet.getString("content");
String picPath= resultSet.getString("picPath");
String author=resultSet.getString("author");
Timestamp timestamp= resultSet.getTimestamp("createDate");
Date createDate=new Date(timestamp.getTime());
System.out.println("id:"+id+" summary:"+summary);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//关闭资源
closeResource(resultSet,preparedStatement,connection);
}
}