81,094
社区成员
发帖
与我相关
我的任务
分享
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.mysql.jdbc.Driver").newInstance();
String url ="jdbc:mysql://localhost/new2007?user=root&password=123&useUnicode=true&characterEncoding=8859_1";
//testDB为你的数据库名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from tb_user";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
package com;
import java.sql.*; //导入数据库操作的类
public class JDBConnection //构造方法,初始化
{
private Connection conn; //连接对象
private Statement stmt; //语句对象
private ResultSet rs; //结果集对象
private String accessdriver; //保存Access驱动程序字符串
private String accessURL; //保存Access连接字符串
public JDBConnection()
{
//Access驱动程序
accessdriver = "com.mysql.jdbc.Driver";
//连接字符串
accessURL = "jdbc:mysql://localhost/new2007?user=root&password=123&useUnicode=true&characterEncoding=8859_1";
conn = null;
}
//该方法从参数得到数据库路径,并加到连接字符串后面,然后再建立连接
public Connection getConnToAccess(){
try{
Class.forName(accessdriver);
conn = DriverManager.getConnection(accessURL);
}catch(Exception e){
System.out.println("操作数据库出错,请仔细检查");
System.err.println(e.getMessage());
}
return conn;
}
//关闭数据库连接
public void close()
{
try{
//rs.close();
//stmt.close();
conn.close();
}catch(SQLException sqlexception){
sqlexception.printStackTrace();
}
}
}
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<jsp:useBean id="DBConn" scope="request" class="com.JDBConnection"/>
<html>
<body>
<%
Connection conn= DBConn.getConnToAccess();
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from tb_user";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
package com;
import java.sql.*;
public class DB {
private static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager
.getConnection("jdbc:mysql://localhost:/bbs?user=root&password=root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
private static Statement getStmt(Connection conn) {
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
private static ResultSet getRs(Statement stmt, String sql) {
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public static void closeConn(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
public static void closeStmt(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
}
public static void closeRs(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
}
}