jsp-sqlserver代码

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();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值