package com.user;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.*;
public class userBean {
private String userId;
private String userName;
private String password;
private String email;
private int count;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setUserId(String userId) {
this.userId = userId;
}
public void setPassword(String password) {
this.password = password;
}
public void setCount(int count) {
this.count = count;
}
public String getPassword() {
return password;
}
public String getUserId() {
return userId;
}
public String getUserName() {
return userName;
}
public int getCount() {
return count;
}
/**
* 获取连接
*/
private Connection conn = null;
private void getConnection() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"
;
try {
conn = DriverManager.getConnection("jdbc
dbc:ues"
;
} catch (SQLException ex1) {
}
// Statement stmt;
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}
/**
* 检查用户名是否可用的方法
* @return 用户的个数
*/
public void checkUserName() {
int i = 0;
this.getConnection();
String sql = "select count(*) from userreg where username='" +
this.userName + "'";
System.out.println(sql);
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
i = rs.getInt(1);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
this.setCount(i);
System.out.println("检查用户名:" + count);
// return count;
}
/**
* 添加用户的方法
* @return 用户插入的行数
*/
public void addUser() {
this.getConnection();
String sql = "insert into userreg (username,password,email) values('" +
this.userName + "','" + this.password + "','" + this.email +
"')";
System.out.println(sql);
try {
Statement stmt = conn.createStatement();
this.count = stmt.executeUpdate(sql);
} catch (SQLException ex) {
ex.printStackTrace();
}
System.out.println( this.count );
}
}
只是 添加用户的方法,用户插入的行数是-1,你们说会不会是myEclipse6.5没有安装好哦,我在其他机子上用5.5打开有是1,不知道为什么。
这篇博客探讨了在Java中使用JDBC进行数据库操作时遇到的问题。作者在尝试添加用户时发现返回的行数为-1,怀疑可能是myEclipse环境配置问题。博客中展示了获取数据库连接、检查用户名可用性和添加用户的方法,但遇到了执行SQL语句时的异常。作者对比了在不同版本myEclipse上的行为差异,并寻求帮助找出问题根源。

6858

被折叠的 条评论
为什么被折叠?



