jsp和mysql连接
##########################################################################
中文问题:
jsp和mysql连接的时候,要在每一个jsp文件中加:
如果是表单的提交要加上:
jdbc:mysql://127.0.0.1:3306/publish?useUnicode=true&characterEncoding=GBK" 和
############################################################################
1.
java.sql.Connection sqlConn;
java.sql.Statement sqlStmt;
java.sql.ResultSet sqlRst;
//regiester jdbc driver
String DBUser="root";
String DBPassword="root";
String DBServer="127.0.0.1"; // Can't use localhost , you must use IP or CNAME
String DBNAME="publish"; // change to your db name
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//connect to the database
sqlConn=java.sql.DriverManager.getConnection
("jdbc:mysql://"+DBServer+":3306/"+DBNAME+"?useUnicode=true&characterEncoding=gb2312",
DBUser,DBPassword);
//create the driver handle
sqlStmt=sqlConn.createStatement
(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
//excute sql
sqlRst=sqlStmt.executeQuery("select * from book");
%>
JSP-JDBC-MYSQL TESTINGID | Book name |
---|---|
//sqlRst.close();
sqlStmt.close();
sqlConn.close();
%>
………………………………………………………………………………
2.
java.sql.Connection conn;
java.lang.String strConn;
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn= java.sql.DriverManager.getConnection("jdbc:mysql://127.0.0.1/test","root","");
%>
………………………………………………………………………………
3.
out.println(" Border is me !");
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}
catch (Exception E) {
out.println("Unable to load driver.");
}
%>
………………………………………………………………………………
4. 若您觉得满意,想转载或收藏这篇文章,我非常感谢,但请您注明作者 border ( border@hacker.cn )
最后欢迎大家访问 --中国安全信息网-- http://www.hacker.cn/
分享到:
2005-03-20 14:16
浏览 540
评论