日期:2014-05-17 浏览次数:21477 次
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class mysql {
private Connection conn = null;
PreparedStatement statement = null;
// connect to MySQL
void connSQL() {
String url = "jdbc:mysql://localhost:3306/hqyl?characterEncoding=UTF-8";
String username = "root";
String password = "root"; // 加载驱动程序以连接数据库
try {
Class.forName("com.mysql.jdbc.Driver" );
conn = DriverManager.getConnection( url,username, password );
}
//捕获加载驱动程序异常
catch ( ClassNotFoundException cnfex ) {
System.err.println(
"装载 JDBC/ODBC 驱动程序失败。" );
cnfex.printStackTrace();
}
//捕获连接数据库异常
catch ( SQLException sqlex ) {
System.err.println( "无法连接数据库" );
sqlex.printStackTrace();
}
}
// disconnect to MySQL
void deconnSQL() {
try {
if (conn != null)
conn.close();
} catch (Exception e) {
System.out.println("关闭数据库问题 :");
e.printStackTrace();
}
}
// execute selection language
ResultSet selectSQL(String sql) {
Res