package mybean;
import java.sql.*;
public class Jdbc{
Connection dbConn;
String username;
String userpsd;
ResultSet rs = null;
Statement stmt = null;
public String getusername() {
return username;
}
public String getuserpsd() {
return userpsd;
}
public void consqlserver(String databasename) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName="
+ databasename; // 连接服务器和数据库test
String userName = "sa"; // 默认用户名
String userPwd = "19880129"; // 密码
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!"); // 如果连接成功
stmt = dbConn.createStatement();
// 控制台输出Connection
// Successful!
} catch (Exception e) {
e.printStackTrace();
}
}
public void querydata(String datesheet) {
String sql = "select * from " + datesheet + ";";// 查询语句
try {
rs = stmt.executeQuery(sql);
while (rs.next()) {// 输出显示
username = (String) rs.getObject(1);
userpsd = (String) rs.getObject(2);
System.out.println(username);
System.out.println(userpsd);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insertdata(String datesheet, String inform[]) throws Exception {
String sql = "insert into "
+ datesheet
+ "(useid,passsword,quepass,tishi,wentian,suozaidi,userdanwei,shenqinren,phonenember,address,mail) values('"
+inform[0] + "','" + inform[1] + "','" + inform[2] + "','"
+ inform[3] + "','" + inform[4] + "','" + inform[5] + "','"
+ inform[6] + "','" + inform[7] + "','" + inform[8] + "','"
+ inform[9] + "','" + inform[10] + "');";
try {
for(int i=0;i<11;i++){
System.out.print(inform[i]);
}
stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void deletedata(String datesheet,String useid) {
String sql = "delete from " + datesheet + " where useid="+useid;
try {
stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void update(String datesheet) {
String sql = "update " + datesheet
+ " set password='qwer' where useid='liu';";
try {
stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}