package com.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DaoTool {
String driver = "com.mysql.jdbc.Driver"; // 驱动程序名
String url = "jdbc:mysql://localhost/test"; //
URL指向要访问的数据库名scutcs
String user = "root"; // MySQL配置时的用户名
String password = "root"; // MySQL配置时的密码
public DaoTool(){
}
public Connection getConn(){
Connection conn = null;
try {
Class.forName(driver); //
加载驱动程序
conn =
DriverManager.getConnection(url, user, password); // 连续数据库
}catch (Exception e) {
System.out.println("连接失败");
e.printStackTrace();
}
return conn;
}
public ResultSet query(String sql,Connection conn)
throws SQLException{
Statement state = conn.createStatement();
ResultSet rs = state.executeQuery(sql);
return rs;
}
public int update(String sql,Connection conn) throws
SQLException{
Statement state = conn.createStatement();
System.out.println(sql);
int result =
state.executeUpdate(sql);//不为零操作失败
return result;
}
}