📄 registerdb.java
字号:
package ebookshop.servlet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//import java.sql.Statement;
public class RegisterDB
{
public RegisterDB(){}
public String InsertData(String userName,String passWord,String realName,String sex,String email)
{
System.out.println("-------------hello2---------------------");
String sqlStr="INSERT INTO customer (userName,passWord,realName,sex,email) VALUES('"+userName+
"','"+passWord+"','"+realName+"','"+sex+"','"+email+"'"+")";
// "+"where userName=\""+userName+"\" "+"and"+" "+"passWord='"+passWord+"'";
String sql="select cid from customer where userName='"+userName+"'";
Connection con=null;
PreparedStatement pst =null;
ResultSet rs =null;
String response="error";
try {
Class.forName("org.gjt.mm.mysql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
//return;
}
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" +
"netbookdata?useUnicode=true&characterEncoding=gb2312","root","abing");
pst = con.prepareStatement(sqlStr);
con.setAutoCommit(false); //取消自动提交
System.out.println("hello1");
pst.execute();
con.commit(); //提交
con.setAutoCommit(true); //设为自动提交
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
if(rs.next()){
response=rs.getInt("cid")+"";
System.out.println(response);
}
return response;
}
catch (SQLException e) {
e.printStackTrace();
//return;
}
finally
{
if(rs != null)
try
{
rs.close();
}
catch(Exception e){e.getMessage();}
/*
if(pst != null)
try
{
pst.close();
}
catch(Exception e){e.getMessage();}*/
if(con != null)
try
{
con.close();
}
catch(Exception e){e.getMessage();}
}
return response;
//return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -