我最近新接触JavaEE。结果我不知道怎么办,贴上代码。
这是Java代码(Connection类)
import java.sql.*;
public class Connection {
public static void connect(String username,String password){
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/sys","root","ASD123zxc,./");
String sql="insert into table1 values ('"+username+"','"+password+"');";
Statement sta=((java.sql.Connection) con).createStatement();
sta.execute(sql);
sta.close();
((java.sql.Connection) con).close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是index.jsp的html代码(我这里只显示body部分的代码)
<table border="0">
<tr>
<th>用户名</th>
<th><input type="text" name="username"></input></th>
<tr>
<th>密码</th>
<th><input type="password",name="password"></input></th>
</tr>
<tr>
<th align="center">
<button type="submit" name="sure">确定</button>
</th>
</tr>
</table>
我的目的是在用户名和密码框里输入信息,然后点击确定按钮,用户名和密码框里的内容会插入到mysql数据库中,应该怎么做呢?