黑马程序员技术交流社区
标题:
谁有JDBC操作数据库的操作帮助类?
[打印本页]
作者:
时光♪微凉
时间:
2014-5-8 22:23
标题:
谁有JDBC操作数据库的操作帮助类?
本帖最后由 时光♪微凉 于 2014-5-10 17:52 编辑
加必要说明的注释,谢谢!
作者:
周大侠
时间:
2014-5-8 23:14
import java.sql.*;
public class Test_JDBC1{
public static void main(String args[])throws Exception{
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("加载数据库驱动程序成功");
}
catch(Exception e)
{
System.out.println("无法加载数据库驱动程序");
}
//创建数据库连接
try{
Connection conn= DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=students","tct","123456");
if(!conn.isClosed())
System.out.println("数据库连接成功!");
//为了执行SQL语句,创建一个语句对象(Statement是一个接口),通过语句对象来执行一条SQL语句
Statement st=conn.createStatement();
//获取数据的记录
String query="select * from Table1";
//使用executeQuery()方法执行SQL命令
ResultSet rs=st.executeQuery(query);
System.out.println("*************************");
while(rs.next())
{
System.out.print(rs.getString("name")+" ");
System.out.print(rs.getInt("age")+" ");
System.out.println(rs.getString("sex")+" ");
}
String sql="insert into Table1 values('Tom','20','man')";
int numberOfRows = st.executeUpdate(sql);
//String strUpdate="UPDATE Table1 SET name='tct4'";
//int rows=st.executeUpdate(strUpdate);
// System.out.println("数据库表中的"+rows+"行被修改了");
//String strDelete="DELETE FROM Table1 WHERE name='Tom'";
// int numberOfRows = st.executeUpdate(strDelete);
//如果记录为空,加入十条记录
/* if(resultSet.getInt()==0)
for(int i=1;i<11;i++)
String sql="insert into faqs value"*/
//执行查询(select)语句,将结果放入到结果集ResultSe(接口)中 */
query="select * from Table1";
//使用executeQuery()方法执行SQL命令
rs=st.executeQuery(query);
System.out.println("*************************");
while(rs.next())
{
System.out.print(rs.getString("name")+" ");
System.out.print(rs.getInt("age")+" ");
System.out.println(rs.getString("sex")+" ");
}
rs.close();
st.close();
conn.close();
}
catch(SQLException ee)
{
System.out.println("数据库连接失败!或存在其他问题");
}
}
}
你看看有用没?
复制代码
作者:
itpower
时间:
2014-5-8 23:30
DBUtils?c3p0?
作者:
时光♪微凉
时间:
2014-5-9 07:52
周大侠 发表于 2014-5-8 23:14
感觉这个不是我想要的那个,我想的是在我对数据库执行增删改查操作时,可以调用JDBC帮助类的方法,传入必要的参数,得到想要的结果,就是可以提高代码复用性的那种
作者:
时光♪微凉
时间:
2014-5-10 10:16
嘿嘿,自己整了一个数据连接帮助类,看看还能优化没
package basedao;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 数据库连接于关闭工具类
*
* @author Administrator
*
*/
public class BaseDao {
//数据库驱动字符串
private String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//连接URL字符串
private String url = "jdbc:sqlserver://localhost:1433;DataBaseName=Message";
//数据库用户名
private String user = "sa";
//用户密码
private String password = "1213";
/**
* 数据连接对象
*/
protected Connection conn;
/**
* 数据操作对象
*/
protected PreparedStatement ps;
/**
* 存储过程操作对象
*/
protected CallableStatement cs;
/**
* 结果集
*/
protected ResultSet rs;
/**
* 创建数据连接对象
*/
public Connection openConnection() {
if (conn == null)
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭数据连接
*/
public void closeConnection() {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (ps != null)
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 关闭数据连接
*/
public void closeConnection(Connection conn, PreparedStatement ps, ResultSet rs) {
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (ps != null)
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 增删改操作
*/
public int executeIDS(String preparedSql, Object... param) {
openConnection();
try {
ps = conn.prepareStatement(preparedSql);
if (param != null)
for (int i = 0; i < param.length; i++) {
// 给Sql语句中的参数赋值
ps.setObject(i + 1, param[i]);
}
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
return -1;
} finally {
closeConnection();
}
}
/**
* 查询操作
*/
public ResultSet findTable(String preparedSql, Object... param) {
openConnection();
try {
ps = conn.prepareStatement(preparedSql);
if (param != null)
for (int i = 0; i < param.length; i++) {
// 给Sql语句中的参数赋值
ps.setObject(i + 1, param[i]);
}
return ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
return null;
}//这后面应该不用关闭,关闭了数据库就无法返回ResultSet结果了
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2