JDBC

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @(#)DataBase.java 010/04/22
* @author Dudu
* EST.All rights reserved
*/
public class DataBase {
/**定义一个Connection 用来连接数据库*/
private Connection conn=null;
/**连接数据库的URL*/
private final String url="jdbc:mysql://localhost/lyvee";
/**指定数据的用户名和密码*/
private final String username="root";
private final String password="123";
/**定义一个int记录更新的记录数量*/
int count=0;
/**定义一个结果集 用于放回查询结果*/
private ResultSet resultSet=null;
private PreparedStatement pstmt=null;
public DataBase(){
conn = connectionDB();
}
/**
* 建立数据的连接
* @exception SQLException, ClassNotFoundException
*/
@SuppressWarnings("finally")
public Connection connectionDB(){
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url,username,password);
System.out.println("连接数据库成功");
}catch(Exception e){
e.printStackTrace();
System.out.println("建立数据库发生错误!");
}finally{
return conn;
}
}
/**
* 查询方法
* @param sql查询sql语句
* @return resultSet
*/
@SuppressWarnings("finally")
public ResultSet query(String sql){
try {
pstmt = conn.prepareStatement(sql);
/**查询*/
resultSet = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}finally{
return resultSet;
}
}
/**
* 更新数据
* @param sql 更新sql语句
* @return
*/
public int update(String sql){
try {
pstmt = conn.prepareStatement(sql);
count=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("执行更新出错了");
}

return count;

}
/**关闭连接*/
public boolean coles(){
boolean isColes = false;
if(resultSet!=null){
try {
resultSet.close();
resultSet=null;
isColes=true;
} catch (SQLException e) {
isColes=false;
e.printStackTrace();
System.out.println("关闭结果集发生错误");
}
}
if(pstmt!=null){
try {
pstmt.close();
pstmt=null;
isColes=true;
} catch (SQLException e) {
isColes=false;
e.printStackTrace();
System.out.println("关闭pstmt发生异常");
}
}
if(conn!=null){
try{
conn.close();
conn=null;
isColes=true;
}catch (Exception e) {
isColes=false;
e.printStackTrace();
System.out.println("关闭conn发生异常");
}
}
return isColes;
}
/**
* 测试查询的方法
* @throws SQLException
*/
public void testQuery() throws SQLException{
resultSet =query("select * from lyvee");
if(resultSet.next()){
System.out.println(resultSet.getString(1));
System.out.println(resultSet.getString(3));
System.out.println(resultSet.getString(2));
}
}
public void testUpdate(){
count = update("insert into lyvee(lname,lpwd,lsex)values('tanliang','aaa111','sex')");
if(count>0){
System.out.println("更新成功");
}
}
/**
*
* @param args
* @throws SQLException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws SQLException, ClassNotFoundException {
DataBase db = new DataBase();
/**调用查询方法*/
//db.testQuery();
/**调用更新方法*/
db.testUpdate();
/**调用关闭连接方法*/
db.coles();

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值