JAVABEAN中的连接代码如下:
try {
Class.forName("org.gjt.mm.mysql.Driver");
String user = rb.getString("db.mysqluser");
String password = rb.getString("db.mysqlpassword");
String url = rb.getString("db.mysqlurl");
//解决中文字符的问题
//url += "?useUnicode=true&characterEncoding=gb2312";
Connection con = DriverManager.getConnection(url,user,password);
return con;
} catch(ClassNotFoundException cnfExc) {
out.println(cnfExc.getMessage());
cnfExc.printStackTrace();
} catch(SQLException sqlExc) {
out.println(sqlExc.getMessage());
sqlExc.printStackTrace();
} catch(Exception e) {
out.println(e.getMessage());
e.printStackTrace();
}
err.println("出异常啦,返回回值!"); //测试连接成功没的,下同。
return null;
}
下面是在JSP页面中的代码:
try {
out.println("000");
Connection conn = DBConnection.getConnection();
out.println(conn);
out.println("123");
Statement stmt = conn.createStatement();
out.println("456");
String sqlCommand = "insert into users values'(" + ID
+ "', '" + password + "', '" + "', '" + sex + "')";
stmt.execute(sqlCommand);
} catch(NullPointerException e) {
out.println(e.getMessage());
e.printStackTrace();
} catch(IOException ioExc) {
out.println(ioExc.getMessage());
ioExc.printStackTrace();
} catch(SQLException sqlExc) {
out.println(sqlExc.getMessage());
sqlExc.printStackTrace();
}
//ResultSet rs = stmt.executeQuery(sqlCommand);