2014-07-13 16:18:25
来 源
中存储网
MySQL
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;
}
}
声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。