the code
下载MYSQL_CONNECTOR包之后,将它放入jre/lib/ext/下
不需修改CLASSPATH和其他任何配置(第三方包我觉得都放在那个目录下).
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
class DBConnection {
private static Statement stmt;
private static DBConnection dbCon;
private DBConnection(){
try{Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException e)
{
System.out.println("数据库驱动加载出错");
}
try{Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost/design?useUnicode=true&characterEncoding=gb2312&mysqlEncoding=gb2312", "dbname","123456");
stmt= conn.createStatement();
}
catch(SQLException e)
{
System.out.println("数据库连接出错OR");
System.out.println("SQL对象语句创建出错"+e);
}
}
public static DBConnection getDBCon(){
dbCon=new DBConnection();
return dbCon;
}
public Statement getStm()
{
return stmt;
}
}
/*
//stmt.execute("insert into test (id) values (23)");
//stmt.execute("insert into test id 'UTF8简体中文' ");
ResultSet rs = stmt.executeQuery("select * from test");
while(rs.next()){
System.out.println(rs.getInt(1));
}
rs.close();
conn.close();
} catch( ClassNotFoundException e){System.out.println(e.getMessage());}
catch (SQLException ex){
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
}