java如何验证用户名是否存在于数据库的代码
publicstaticintinsert(Useruser)throwsException{//数据库驱动System.out.println(user.getUser...
public static int insert(User user) throws Exception{
//数据库驱动
System.out.println(user.getUsername());
System.out.println("密码" +user.getPassword());
System.out.println(user.getSex());
System.out.println(user.getHobby());
System.out.println("个人简介"+user.getBook());
System.out.println("邮箱"+user.getEmail());
System.out.println("城市"+user.getCity());
Class.forName("com.mysql.jdbc.Driver");
try {
//根据连接信息创建一个连接
conn = (Connection) DriverManager.getConnection(url, JDBCname, JDBCpassword);
//插入数据的sql语句
String sql = "insert into user (username,password,sex,hobby,book,email,city) values (?,?,?,?,?,?,?)";
//使用预处理连接
//String sql ="insert into user(username,password,name,age,weight,hobby) values('$_POST[user]','$_POST[sex]','$_POST[pwd]','$_POST[select]','$str', '$path','$_POST[intro]')";
pstmt = (PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getSex());
pstmt.setString(4, user.getHobby());
pstmt.setString(5, user.getBook());
pstmt.setString(6, user.getEmail());
pstmt.setString(7, user.getCity());
//pstmt.setString(7, user.getDescribe());
//插入成功会返回int类型的值
int i = pstmt.executeUpdate();
if (i > 0) {
System.out.println("成功插入了" + i + "条数据");
return i;
}else{
System.out.println("插入失败");
return i;
}
} catch (Exception e) {
//打印异常
e.printStackTrace();
//finally不管是否出错都会进行
} finally {
//从小到大关闭资源
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
}
return 0;
} 展开
//数据库驱动
System.out.println(user.getUsername());
System.out.println("密码" +user.getPassword());
System.out.println(user.getSex());
System.out.println(user.getHobby());
System.out.println("个人简介"+user.getBook());
System.out.println("邮箱"+user.getEmail());
System.out.println("城市"+user.getCity());
Class.forName("com.mysql.jdbc.Driver");
try {
//根据连接信息创建一个连接
conn = (Connection) DriverManager.getConnection(url, JDBCname, JDBCpassword);
//插入数据的sql语句
String sql = "insert into user (username,password,sex,hobby,book,email,city) values (?,?,?,?,?,?,?)";
//使用预处理连接
//String sql ="insert into user(username,password,name,age,weight,hobby) values('$_POST[user]','$_POST[sex]','$_POST[pwd]','$_POST[select]','$str', '$path','$_POST[intro]')";
pstmt = (PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getSex());
pstmt.setString(4, user.getHobby());
pstmt.setString(5, user.getBook());
pstmt.setString(6, user.getEmail());
pstmt.setString(7, user.getCity());
//pstmt.setString(7, user.getDescribe());
//插入成功会返回int类型的值
int i = pstmt.executeUpdate();
if (i > 0) {
System.out.println("成功插入了" + i + "条数据");
return i;
}else{
System.out.println("插入失败");
return i;
}
} catch (Exception e) {
//打印异常
e.printStackTrace();
//finally不管是否出错都会进行
} finally {
//从小到大关闭资源
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
}
return 0;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询