private ResultSet rs;
private static final String drivername = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;
DatabaseName=My_website;user=tym;password=tym;";
public static synchronized Connection getConn() throws Exception {
try {
Class.forName(drivername);
con = DriverManager.getConnection(url);
return con;
} catch (SQLException e) {
System.err.println(e.getMessage());
throw e;
}
}
自定义数据库操作函数:
public ResultSet query(String sql){//查询语句
ResultSet rs=null;
try{
Connection com = getConnection();
Statement stmt = conn.creatStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex){
System.err.println(ex.getMessage());
}
return rs;
public voik insert(){//插入语句
try{
StringBuffer q = new StringBuffer(200);
q.append(INSERT INTO
userinfo(uid,password,realName,gender,email,tel,question,answer)
VALUES(“);
q.append(“’”+uid+”’ ,‘”);
q.append(passwork+”’,’”);
q.append(realName+”’,’”);
q.append(gender+”’,’”);
q.append(email+”’,’”);
q.append(tel+”’,’”);
q.append(question+”’,’”);
q.append(answer+”’)’”);
update(q.toSring());
}catch(Exception e){
System.err.println(e.getMessage());
}
}
public int update(String sql){//更新语句
int result = 0;
try{
Connection conn = getConnection();
Statement stmt = conn.createStatement();
result = stmt.executeUpdate(sql);
<< 上一页 [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] ... 下一页 >>