【发布时间】:2019-09-13 18:23:39
【问题描述】:
这是我的 servlet 的代码,它从注册表单中接收用户名参数
String tusername=request.getParamater("un");
String dbURL="db.com";
String dbusername= "lc";
String dbpassword="lcpw";
Connection con=(Connection) DriverManager.getConnection(dbURL,dbusername,dbpassword);
Statement stmt= con.createStatement();
String query="SELECT * FROM users.username WHERE username=(tusername)";
ResultSet rs= stmt.executeQuery(query);
if(rs.next()==false){
//create new userobject with value of tusername
}
我的问题是如何使用 tusername 创建一个新的用户对象,会是这样吗?
if(rs.next()==false){
Statement stmt=con.createStatament();
String query="INSERT INTO user.username VALUE 'tusername'";
ResultSet rs= stmt.updateQuery(query);
}
我知道其中一些可能是过时的(例如不使用准备好的语句),我只是想更好地理解,我认为我有一些小的语法问题,谢谢 :)
【问题讨论】:
-
由于您的代码在servlet中,我建议您在servlet容器(tomcat?jetty?)的配置中注册您的数据源,并通过JNDI获取。