大家可以在这里下载:http://download.csdn.net/detail/zeq9069/4761851
费了我老长时间才根据csdn中的代码高亮弄出来的!!!!废话不说直接看代码:
其中涉及到的文件如下:
test.html代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src=" jquery.js" type="text/javascript"></script>
<link type="text/css" rel="stylesheet" href=" http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/default.css" />
<script type="text/javascript" src=" shCore-src.js"></script>
<script type="text/javascript" src=" article_code.js"></script>
</head>
<body>
<pre class="c++" name="code">
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
private static Connection conn;
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection(){//实现数据库连接方法
try {
//oj为数据库名字,root,root是用户名和密码
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/oj", "root", "root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn,Statement st,ResultSet rs){/关闭数据库链接
try {
if(conn!=null){
conn.close();
}
if(st!=null){
st.close();
}
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
</pre>
</body>
</html>
效果图如下: