package com.aaaa.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBConnection {
public Connection getConn() throws Exception{//得到Connection对象
Connection conn =null;
String url= "jdbc:mysql://localhost:3306/aaaa";//数据库的URL
Class.forName("com.mysql.jdbc.Driver");//装载JDBC驱动程序
conn = DriverManager.getConnection(url,"root","密码");
return conn;
}
public Statement getStmt(Connection conn) throws SQLException{ //得到Statement对象
Statement stmt =null;
stmt = conn.createStatement();
return stmt;
}
public void updateData(String strSql,Statement stmt) throws SQLException{//执行sql语句
stmt.executeUpdate(strSql);
}
public ResultSet execQuery(String sql,Statement stmt) throws SQLException{ //得到查询结果
ResultSet rs=null;
rs=(ResultSet) stmt.executeQuery(sql);
return rs;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBConnection {
public Connection getConn() throws Exception{//得到Connection对象
Connection conn =null;
String url= "jdbc:mysql://localhost:3306/aaaa";//数据库的URL
Class.forName("com.mysql.jdbc.Driver");//装载JDBC驱动程序
conn = DriverManager.getConnection(url,"root","密码");
return conn;
}
public Statement getStmt(Connection conn) throws SQLException{ //得到Statement对象
Statement stmt =null;
stmt = conn.createStatement();
return stmt;
}
public void updateData(String strSql,Statement stmt) throws SQLException{//执行sql语句
stmt.executeUpdate(strSql);
}
public ResultSet execQuery(String sql,Statement stmt) throws SQLException{ //得到查询结果
ResultSet rs=null;
rs=(ResultSet) stmt.executeQuery(sql);
return rs;
}
}