package conn;import java.sql.*;
import javax.swing.JOptionPane;public class DBConnection {
private static final String DRIVER_CLASS = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; // JDBC方式连接数据库
public static Connection getConn() {
Connection con = null;
String user = "sa";
String password = ""; // 加载驱动
try {
Class.forName(DRIVER_CLASS);
} catch (ClassNotFoundException ex) {
} // 建立连接
try {
String url = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=Reservation;"; // JDBC方式连接数据库
// String url = "jdbc:odbc:ttxt"; //ODBC方式连接数据库
con = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "数据库未连接!");
System.exit(1);
}
return con;
} public static PreparedStatement prepare(Connection conn, String sql) {
PreparedStatement pstmt = null;
try {
if(conn != null) {
pstmt = conn.prepareStatement(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static PreparedStatement prepare(Connection conn, String sql, int autoGenereatedKeys) {
PreparedStatement pstmt = null;
try {
if(conn != null) {
pstmt = conn.prepareStatement(sql, autoGenereatedKeys);
}
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static Statement getStatement(Connection conn) {
Statement stmt = null;
try {
if(conn != null) {
stmt = conn.createStatement();
}
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
/*
public static ResultSet getResultSet(Connection conn, String sql) {
Statement stmt = getStatement(conn);
ResultSet rs = getResultSet(stmt, sql);
close(stmt);
return rs;
}
*/
public static ResultSet getResultSet(Statement stmt, String sql) {
ResultSet rs = null;
try {
if(stmt != null) {
rs = stmt.executeQuery(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public static void executeUpdate(Statement stmt, String sql) {
try {
if(stmt != null) {
stmt.executeUpdate(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Connection conn) {
try {
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement stmt) {
try {
if(stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs) {
try {
if(rs != null) {
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}//Test.java
package test;import java.sql.*;
import java.util.Date;import conn.DBConnection;public class test { public static void main(String[] args) {
Connection conn = DBConnection.getConn();
String sql = "insert into User values (null, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = DBConnection.prepare(conn, sql);
try {
String user = "聊聊";
String password = "124";
String phone = "23411";
String addr1 = "";
String addr2 = "";
String adrr3 = "";
Date rdate = new Date();
pstmt.setString(1, user);
pstmt.setString(2, password);
pstmt.setString(3, phone);
pstmt.setString(4, addr1);
pstmt.setString(5, addr2);
pstmt.setString(6, adrr3);
pstmt.setTimestamp(7, new Timestamp(rdate.getTime()));
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.close(pstmt);
DBConnection.close(conn);
} }}数据库表结构:标识 数据类型 长度 定义/相互关系
Id Int 8 自动递增
userName Varchar 50
password Varchar 50
Phone Int 8
Addr1 Varchar 50
Addr2 varchar 50
Addr3 Varchar 50
Rdate Datetime 8 注册日期
Score float 9 积分出现的错误为:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid parameter binding(s).
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.validateParameters(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.validateParameters(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.preImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
at test.test.main(test.java:29)
import javax.swing.JOptionPane;public class DBConnection {
private static final String DRIVER_CLASS = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; // JDBC方式连接数据库
public static Connection getConn() {
Connection con = null;
String user = "sa";
String password = ""; // 加载驱动
try {
Class.forName(DRIVER_CLASS);
} catch (ClassNotFoundException ex) {
} // 建立连接
try {
String url = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=Reservation;"; // JDBC方式连接数据库
// String url = "jdbc:odbc:ttxt"; //ODBC方式连接数据库
con = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "数据库未连接!");
System.exit(1);
}
return con;
} public static PreparedStatement prepare(Connection conn, String sql) {
PreparedStatement pstmt = null;
try {
if(conn != null) {
pstmt = conn.prepareStatement(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static PreparedStatement prepare(Connection conn, String sql, int autoGenereatedKeys) {
PreparedStatement pstmt = null;
try {
if(conn != null) {
pstmt = conn.prepareStatement(sql, autoGenereatedKeys);
}
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static Statement getStatement(Connection conn) {
Statement stmt = null;
try {
if(conn != null) {
stmt = conn.createStatement();
}
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
/*
public static ResultSet getResultSet(Connection conn, String sql) {
Statement stmt = getStatement(conn);
ResultSet rs = getResultSet(stmt, sql);
close(stmt);
return rs;
}
*/
public static ResultSet getResultSet(Statement stmt, String sql) {
ResultSet rs = null;
try {
if(stmt != null) {
rs = stmt.executeQuery(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public static void executeUpdate(Statement stmt, String sql) {
try {
if(stmt != null) {
stmt.executeUpdate(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Connection conn) {
try {
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement stmt) {
try {
if(stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs) {
try {
if(rs != null) {
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}//Test.java
package test;import java.sql.*;
import java.util.Date;import conn.DBConnection;public class test { public static void main(String[] args) {
Connection conn = DBConnection.getConn();
String sql = "insert into User values (null, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = DBConnection.prepare(conn, sql);
try {
String user = "聊聊";
String password = "124";
String phone = "23411";
String addr1 = "";
String addr2 = "";
String adrr3 = "";
Date rdate = new Date();
pstmt.setString(1, user);
pstmt.setString(2, password);
pstmt.setString(3, phone);
pstmt.setString(4, addr1);
pstmt.setString(5, addr2);
pstmt.setString(6, adrr3);
pstmt.setTimestamp(7, new Timestamp(rdate.getTime()));
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.close(pstmt);
DBConnection.close(conn);
} }}数据库表结构:标识 数据类型 长度 定义/相互关系
Id Int 8 自动递增
userName Varchar 50
password Varchar 50
Phone Int 8
Addr1 Varchar 50
Addr2 varchar 50
Addr3 Varchar 50
Rdate Datetime 8 注册日期
Score float 9 积分出现的错误为:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid parameter binding(s).
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.validateParameters(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.validateParameters(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.preImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
at test.test.main(test.java:29)
而在设置值的时候只设置了7个
pstmt.setString(1, user);
pstmt.setString(2, password);
pstmt.setString(3, phone);
pstmt.setString(4, addr1);
pstmt.setString(5, addr2);
pstmt.setString(6, adrr3);
pstmt.setTimestamp(7, new Timestamp(rdate.getTime()));
import java.util.Date;
import java.util.Random;import conn.DBConnection;
import user.User;public class test { public static void main(String[] args) throws SQLException {
Connection conn= DBConnection.getConn();
int disnumber=new Random().nextInt(20);
int dis = new Random().nextInt(20);
float dispercentage = (float)dis/100;
String username = String.valueOf(disnumber);
String password = String.valueOf(dispercentage); String sql = "insert into disqualification( username , password ) values ( "+username+" , "+password+")";
Statement st = DBConnection.getStatement(conn);
st.executeUpdate(sql);
}}表disqualification 表结构为
id int 自动递增
username string
password string
1、自增字段不用赋值;
2、user是关键字。
request.setCharacterEncoding("GBK");
String action = request.getParameter("action");
if(action != null && action.trim().equals("register")) {
String username = request.getParameter("username");
String password = request.getParameter("password");
//String password2 = request.getParameter("password2");
String phone = request.getParameter("phone");
String addr1 = request.getParameter("addr1");
String addr2 = request.getParameter("addr2");
String addr3 = request.getParameter("addr3");
User u = new User();
u.setUsername(username);
u.setPassword(password);
u.setPhone(phone);
u.setAddr1(addr1);
u.setAddr2(addr2);
u.setAddr3(addr3);
u.setRdate(new Date());
u.save();
out.println("注册成功!恭喜!");
return;
}
%>///以下是User.java
package user;import java.sql.*;import java.util.Date;import conn.DBConnection;
public class User { int id; String username; String password; String phone; String addr1;
String addr2;
String addr3; Date rdate;
float score; public String getAddr1() {
return addr1;
} public void setAddr1(String addr1) {
this.addr1 = addr1;
}
public String getAddr2() {
return addr2;
} public void setAddr2(String addr2) {
this.addr2 = addr2;
} public String getAddr3() {
return addr3;
} public void setAddr3(String addr3) {
this.addr3 = addr3;
}
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} public Date getRdate() {
return rdate;
} public void setRdate(Date rdate) {
this.rdate = rdate;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
}
public void save() {
Connection conn = DBConnection.getConn();
System.out.println(username);
String sql = "insert into customer(username,password,phone,addr1,addr2,addr3,rdate,score) values("+username+","+password+","+phone+","+addr1+","+addr2+","+addr3+",getDate(),0)";
Statement statement = DBConnection.getStatement(conn);
try {
statement.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Tomcat报如下错误:
java.sql.SQLException:[Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]在此上下文中不允许使用‘fsfs’。此处只允许使用常量、表达式或变量。不允许使用列名。
PS:fsfs是username,是从Register.jsp中传过来的啊,应该是变量啊。请高手指教