//=========================
//建立mysql数据库连接类
//DBconn.java
//=========================
//连接数据库
import java.sql.*;
import java.util.*;public class DBConn
{
public String sql_driver = "org.gjt.mm.mysql.Driver";
public String sql_url = "jdbc:mysql://localhost:3306";
public String sql_DBName = "mydb";
public String user = "root";
public String pwd = "4414"; Connection conn = null;
Statement stmt = null;
ResultSet rs = null; public boolean SetDriver(String drv)
{
this.sql_driver = drv;
return true;
} public String getDriver()
{
return this.sql_driver;
} public boolean setUrl(String url)
{
this.sql_url = url;
return true;
} public boolean setDBName(String dbname)
{
this.sql_DBName = dbname;
return true;
} public String getDBName()
{
return this.sql_DBName;
} public boolean setUser(String user)
{
this.user = user;
return true;
} public String getUser()
{
return this.user;
} public boolean setpwd(String pwd)
{
this.pwd = pwd;
return true;
} public String getpwd()
{
return this.pwd;
} //构造函数,连接数据库
public DBConn()
{
try{
Class.forName(sql_driver); //加载数据库驱动
this.conn = DriverManager.getConnection(sql_url + "/" + sql_DBName + "?user="+ user +"&password="+ pwd +"&useUnicode=true&characterEncoding=gb2312");
this.stmt = this.conn.createStatement();
}catch(Exception e)
{
System.out.println(e.toString());
}
} public ResultSet executeQuery(String strSql)
{
try{
this.rs = stmt.executeQuery(strSql);
return this.rs;
}catch(SQLException e)
{
System.out.println(e.toString());
return null;
}catch(NullPointerException e)
{
System.out.println(e.toString());
return null;
}
}
//执行数据的插入、修改删除操作
public boolean execute(String strsql)
{
try{
if (this.stmt.executeUpdate(strsql) == 0)
return false;
else
return true;
}
catch(SQLException e)
{
System.out.println(e.toString());
return false;
}
catch(NullPointerException e)
{
System.out.println(e.toString());
return false;
}
}
//结果指针跳转某一行
public boolean rs_absolute(int row)
{
try{
this.rs.absolute(row);
return true;
}catch(SQLException e)
{
System.out.println(e.toString());
return false;
}
}
//到最后一行
public void rs_afterlast()
{
try{
this.rs.afterLast();
}catch(SQLException e)
{
System.out.println(e.toString());
}
}
//到前一行
public void rs_beforeFirst()
{
try{
this.rs.beforeFirst();
}catch(SQLException e)
{
System.out.println(e.toString());
}
}
//关闭记录集
public void rs_close()
{
try{
this.rs.close();
}catch(SQLException e)
{
System.out.println(e.toString());
}
}
//删除某一行
public void rs_deleteRow()
{
try{
this.rs.deleteRow();
}catch(SQLException e)
{
System.out.println(e.toString());
}
}
//判断是否为第一行
public boolean rs_first()
{
try{
this.rs.first();
return true;
}catch(SQLException e)
{
System.out.println(e.toString());
return false;
}
}
//得到某一列
public String rs_getString(String column)
{
try{
return this.rs.getString(column);
}catch(SQLException e)
{
System.out.println(e.toString());
return null;
}
}
/*
[名称]:rs_getHtmlString(String column)
[参数]:String column
[作用]:替换回车换行
[时间]:2004-9-4
*/
public String rs_getHtmlString(String column)
{
try{
String str1 = this.rs.getString(column);
String str2 = "\r\n";
String str3 = "<br>";
return this.replaceAll(str1,str2,str3);
}catch(SQLException e)
{
System.out.println(e.toString());
return null;
}
}
/*
[名称]:replaceAll(String str1,String str2,String str3)
[参数]:String str1,String str2,String str3
[作用]: 把str1中的str2替换成str3
[时间]:2004-9-4
*/
public String replaceAll(String str1,String str2,String str3)
{
StringBuffer strBuf = new StringBuffer(str1);
int index = 0;
//在str1中才、查找str2,如果找到,记住他的位置
while(str1.indexOf(str2,index)!=-1)
{
index = str1.indexOf(str2,index);
//在str2的位置开始进行替换,长度为(str2.length)的字符串替换成str3
strBuf.replace(str1.indexOf(str2,index),str1.indexOf(str2,index)+str2.length(),str3);
index = index + str3.length();
str1 = strBuf.toString();
}
return strBuf.toString();
}
//所在列为第机列
public int rs_getInt(String column)
{
try{
return this.rs.getInt(column);
}catch(SQLException e)
{
System.out.println(e.toString());
return -1;
}
}
//取下一条记录
public boolean rs_next()
{
try{
return this.rs.next();
}catch(SQLException e)
{
System.out.println(e.toString());
return false;
}
}
//判断结果中是否有记录
public boolean hasData()
{
try{
boolean has_Data = this.rs.first();
this.rs.beforeFirst();
return has_Data;
}catch(SQLException e)
{
System.out.println(e.toString());
return false;
}
}
//是否是最后一条记录
public boolean rs_last()
{
try{
return this.rs.last();
}catch(SQLException e)
{
System.out.println(e.toString());
return false;
}
}
//上一条记录
public boolean rs_previous()
{
try{
return this.rs.previous();
}catch(SQLException e)
{
System.out.println(e.toString());
return false;
}
}
}
//建立mysql数据库连接类
//DBconn.java
//=========================
//连接数据库
import java.sql.*;
import java.util.*;public class DBConn
{
public String sql_driver = "org.gjt.mm.mysql.Driver";
public String sql_url = "jdbc:mysql://localhost:3306";
public String sql_DBName = "mydb";
public String user = "root";
public String pwd = "4414"; Connection conn = null;
Statement stmt = null;
ResultSet rs = null; public boolean SetDriver(String drv)
{
this.sql_driver = drv;
return true;
} public String getDriver()
{
return this.sql_driver;
} public boolean setUrl(String url)
{
this.sql_url = url;
return true;
} public boolean setDBName(String dbname)
{
this.sql_DBName = dbname;
return true;
} public String getDBName()
{
return this.sql_DBName;
} public boolean setUser(String user)
{
this.user = user;
return true;
} public String getUser()
{
return this.user;
} public boolean setpwd(String pwd)
{
this.pwd = pwd;
return true;
} public String getpwd()
{
return this.pwd;
} //构造函数,连接数据库
public DBConn()
{
try{
Class.forName(sql_driver); //加载数据库驱动
this.conn = DriverManager.getConnection(sql_url + "/" + sql_DBName + "?user="+ user +"&password="+ pwd +"&useUnicode=true&characterEncoding=gb2312");
this.stmt = this.conn.createStatement();
}catch(Exception e)
{
System.out.println(e.toString());
}
} public ResultSet executeQuery(String strSql)
{
try{
this.rs = stmt.executeQuery(strSql);
return this.rs;
}catch(SQLException e)
{
System.out.println(e.toString());
return null;
}catch(NullPointerException e)
{
System.out.println(e.toString());
return null;
}
}
//执行数据的插入、修改删除操作
public boolean execute(String strsql)
{
try{
if (this.stmt.executeUpdate(strsql) == 0)
return false;
else
return true;
}
catch(SQLException e)
{
System.out.println(e.toString());
return false;
}
catch(NullPointerException e)
{
System.out.println(e.toString());
return false;
}
}
//结果指针跳转某一行
public boolean rs_absolute(int row)
{
try{
this.rs.absolute(row);
return true;
}catch(SQLException e)
{
System.out.println(e.toString());
return false;
}
}
//到最后一行
public void rs_afterlast()
{
try{
this.rs.afterLast();
}catch(SQLException e)
{
System.out.println(e.toString());
}
}
//到前一行
public void rs_beforeFirst()
{
try{
this.rs.beforeFirst();
}catch(SQLException e)
{
System.out.println(e.toString());
}
}
//关闭记录集
public void rs_close()
{
try{
this.rs.close();
}catch(SQLException e)
{
System.out.println(e.toString());
}
}
//删除某一行
public void rs_deleteRow()
{
try{
this.rs.deleteRow();
}catch(SQLException e)
{
System.out.println(e.toString());
}
}
//判断是否为第一行
public boolean rs_first()
{
try{
this.rs.first();
return true;
}catch(SQLException e)
{
System.out.println(e.toString());
return false;
}
}
//得到某一列
public String rs_getString(String column)
{
try{
return this.rs.getString(column);
}catch(SQLException e)
{
System.out.println(e.toString());
return null;
}
}
/*
[名称]:rs_getHtmlString(String column)
[参数]:String column
[作用]:替换回车换行
[时间]:2004-9-4
*/
public String rs_getHtmlString(String column)
{
try{
String str1 = this.rs.getString(column);
String str2 = "\r\n";
String str3 = "<br>";
return this.replaceAll(str1,str2,str3);
}catch(SQLException e)
{
System.out.println(e.toString());
return null;
}
}
/*
[名称]:replaceAll(String str1,String str2,String str3)
[参数]:String str1,String str2,String str3
[作用]: 把str1中的str2替换成str3
[时间]:2004-9-4
*/
public String replaceAll(String str1,String str2,String str3)
{
StringBuffer strBuf = new StringBuffer(str1);
int index = 0;
//在str1中才、查找str2,如果找到,记住他的位置
while(str1.indexOf(str2,index)!=-1)
{
index = str1.indexOf(str2,index);
//在str2的位置开始进行替换,长度为(str2.length)的字符串替换成str3
strBuf.replace(str1.indexOf(str2,index),str1.indexOf(str2,index)+str2.length(),str3);
index = index + str3.length();
str1 = strBuf.toString();
}
return strBuf.toString();
}
//所在列为第机列
public int rs_getInt(String column)
{
try{
return this.rs.getInt(column);
}catch(SQLException e)
{
System.out.println(e.toString());
return -1;
}
}
//取下一条记录
public boolean rs_next()
{
try{
return this.rs.next();
}catch(SQLException e)
{
System.out.println(e.toString());
return false;
}
}
//判断结果中是否有记录
public boolean hasData()
{
try{
boolean has_Data = this.rs.first();
this.rs.beforeFirst();
return has_Data;
}catch(SQLException e)
{
System.out.println(e.toString());
return false;
}
}
//是否是最后一条记录
public boolean rs_last()
{
try{
return this.rs.last();
}catch(SQLException e)
{
System.out.println(e.toString());
return false;
}
}
//上一条记录
public boolean rs_previous()
{
try{
return this.rs.previous();
}catch(SQLException e)
{
System.out.println(e.toString());
return false;
}
}
}
//注册用户
//reg.java
//=================================
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;public class reg extends HttpServlet
{
private static final String CONTENT_TYPE="text/html;charset=GBK";
public void init() throws ServletException
{
}
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
{
resp.setContentType(CONTENT_TYPE);
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head><title>reg</title></head>");
out.println("<body>");
out.println("<p>The servlet has recevied a GET This is the reply");
out.println("</body>");
out.println("</html>");
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
{
resp.setContentType(CONTENT_TYPE);
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head><title>用户注册</title></head>");
out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">");
out.println("<body>");
String logname,realname,password1,password2,email,gender,phone;
String problem,answer,province,education,selfintro,hobby;
String[] hobbies;
boolean regAttemp = false;
String errorMessge = "";
HttpSession session = req.getSession(true);
logname = req.getParameter("logname");
realname = req.getParameter("realname");
password1= req.getParameter("password1");
password2= req.getParameter("password2");
email = req.getParameter("email");
gender = req.getParameter("gender");
phone = req.getParameter("phone");
if (phone.trim().equals(""))
{
phone = null;
}
problem = req.getParameter("problem");
if (problem.trim().equals(""))
{
problem = null;
}
answer = req.getParameter("answer");
if (answer.trim().equals(""))
{
answer = null;
}
province = req.getParameter("province");
if (province.trim().equals(""))
{
province = null;
}
education = req.getParameter("education");
if (education.trim().equals(""))
{
education = null;
}
hobbies = req.getParameterValues("hobbies");
hobby = "";
if (hobbies!=null)
{
for (int i=0;i<hobbies.length;i++)
{
hobby = hobby + ; + hobbies[i];
}
}else
{
hobby = "null";
}
selfintro = req.getParameter("selfintro");
if (selfintro.trim().equals(""))
{
selfintro = "";
}
//转化中文
logname = getstr(logname);
realname = getstr(realname);
password1= getstr(password1);
email = getstr(email);
gender = getstr(gender);
phone = getstr(phone);
problem = getstr(problem);
answer = getstr(answer);
province = getstr(province);
education= getstr(education);
hobby = getstr(hobby);
selfintro= getstr(selfintro);
//String txtsql = "select * from member where logname='"+logname+"'";
DBConn conn = new DBConn();
if (this.ExistsUser(logname))
{
out.println("对不起,已经有人注册了");
resp.sendRedirect("../reg.htm");
}
else
{
String strsql = "insert into member (logname,pwd,email,gender,phone,problem,answer,province,education,hobbies,selfintro) values ('"+logname+"','"+password1+"','"+email+"','"+gender+"','"+phone+"','"+problem+"','"+answer+"','"+province+"','"+education+"','"+hobby+"','"+selfintro+"')";
conn.execute(strsql);
out.println("添加成功");
}
}
//检测是否存在用户
public boolean ExistsUser(String username)
{
try{
DBConn myconn = new DBConn();
myconn.executeQuery("select * from member where logname='"+username+"'");
if(myconn.rs_next())
{
return true;
}
else
{
return false;
}
}
catch(Exception e)
{
return false;
}
}
//转化中文处理函数
public String getstr(String str)
{
try{
String temp_p = str;
byte[] temp_t = temp_p.getBytes("ISO8859-1");
String temp = new String(temp_t);
return temp;
}catch(Exception e)
{
}
return "null";
}
//释放
public void destroy()
{
}
}
你还不要忘了加mysql的驱动!
1>选与数据库建立连接 得到Connection对象(数据库连接对象)
javax.sql.DataSource ds;
dataSource = getDataSource(request); //从struts-config.xml的DataSource得到连接数据代码
java.sql.Connection conn;
conn = dataSource.getConnection();2>然后生成PreparedStatement对象,执行SQL语句,返回结果集
java.sql.PreparedStatement ps;
java.sql.ResultSet rs;
String sql = "select * from userInfo where username = ?"; //SQL语句,一个查找数据库表语句
String username = "admin";
ps = conn.prepareStatement(sql); //执行SQL语句
ps.setString(1, username);
rs = ps.executeQuery(); //得到记录集System.out.println(rs.getString("password")); //打印密码具体细节参看楼上的,你先用MYSQL客户端创建一个userInfo表