求jsp与mysql连接的代码
谢谢大家,请大家给段完整的jsp与mysql连接的代码,就是说从加载驱动、连接数据库、创建数据库和表,删除的……等等了,最好是可以做个固定的模板的那种。谢谢了。我说的是用...
谢谢大家,请大家给段完整的jsp与mysql连接的代码,就是说从加载驱动、连接数据库、创建数据库和表,删除的……等等了,最好是可以做个固定的模板的那种。
谢谢了。
我说的是用jsp连接的哦!!! 展开
谢谢了。
我说的是用jsp连接的哦!!! 展开
展开全部
import java.sql.*;
public class DbOperator {
public final String DB_USER= "root";
public final String DB_PWD = "1234";
public final String DB_HOST = "127.0.0.1";
public final String DB_NAME = "test";
public DbOperator() {
}
/**
* 得到数据库连接
* @return Connection
*/
public Connection getConnection()
{
Connection conn = null;
String url = "jdbc:mysql://"+this.DB_HOST+"/"+this.DB_NAME+"?useUnicode=true&characterEncoding=GBK";
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = java.sql.DriverManager.getConnection(url, this.DB_USER, this.DB_PWD);
}catch(Exception e)
{
e.printStackTrace();
}
return conn;
}
}
使用的
Connection conn = dbOperator.getConnection();
try
{
Statement st = conn.createStatement();
String sql = " select * from user where username ='" + username + "' and pwd ='" + pwd + "'";
ResultSet rs = st.executeQuery(sql);
if(rs.next())
{
userInfo = new UserInfo();
userInfo.setAge(rs.getString("age"));
userInfo.setId(rs.getInt("id"));
userInfo.setPwd(rs.getString("pwd"));
userInfo.setSex(rs.getString("sex"));
userInfo.setTheName(rs.getString("the_name"));
userInfo.setUserName(rs.getString("username"));
}
rs.close();
st.close();
conn.close();
}catch(Exception e)
{
e.printStackTrace();
}
return userInfo;
public class DbOperator {
public final String DB_USER= "root";
public final String DB_PWD = "1234";
public final String DB_HOST = "127.0.0.1";
public final String DB_NAME = "test";
public DbOperator() {
}
/**
* 得到数据库连接
* @return Connection
*/
public Connection getConnection()
{
Connection conn = null;
String url = "jdbc:mysql://"+this.DB_HOST+"/"+this.DB_NAME+"?useUnicode=true&characterEncoding=GBK";
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = java.sql.DriverManager.getConnection(url, this.DB_USER, this.DB_PWD);
}catch(Exception e)
{
e.printStackTrace();
}
return conn;
}
}
使用的
Connection conn = dbOperator.getConnection();
try
{
Statement st = conn.createStatement();
String sql = " select * from user where username ='" + username + "' and pwd ='" + pwd + "'";
ResultSet rs = st.executeQuery(sql);
if(rs.next())
{
userInfo = new UserInfo();
userInfo.setAge(rs.getString("age"));
userInfo.setId(rs.getInt("id"));
userInfo.setPwd(rs.getString("pwd"));
userInfo.setSex(rs.getString("sex"));
userInfo.setTheName(rs.getString("the_name"));
userInfo.setUserName(rs.getString("username"));
}
rs.close();
st.close();
conn.close();
}catch(Exception e)
{
e.printStackTrace();
}
return userInfo;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询