先导入sql驱动包库
核心代码就几行
String URL="jdbc:mysql://localhost:3306/Test";
String user="root";
String pwd="xxx";
String sql="insert Employee(empAccount,empPassword) values(?,?)";
Connection conn=null;
PreparedStatement stmt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(URL, user, pwd);
stmt=conn.prepareStatement(sql);
stmt.setString(1, username);
stmt.setString(2, password);
int result =stmt.executeUpdate();
if(result>0) {
JOptionPane.showMessageDialog(null, "增加成功!", "OK",JOptionPane.INFORMATION_MESSAGE);
}
else {
JOptionPane.showMessageDialog(null, "增加失败!", "NO",JOptionPane.ERROR_MESSAGE);
}
}
catch(Exception e2) {
System.out.println(e2.getMessage());
}
finally {
try {
stmt.close();
}
catch(Exception e1) {
System.out.println(e1.getMessage());
}
}
}