userInfo.java
package com.sql.form;
public class UserInfo {
int userId;
String userName;
String userAddress;
int userAge;
String userSex;
public UserInfo(int id,String name,String addre,int age,String sex){
this.userAddress=addre;
this.userAge=age;
this.userId=id;
this.userName=name;
this.userSex=sex;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
public int getUserAge() {
return userAge;
}
public void setUserAge(int userAge) {
this.userAge = userAge;
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}
public String toString() {
return "address:" + userAddress + "\t" + "age:" + userAge + "\t"
+ "name:" + userName + "\t" + "sex:" + userSex + "\t" + "id:"
+ userId;
}
}
UserSQLConn.java
package com.sql.test;
import java.sql.*;
import java.util.*;
import com.sql.form.UserInfo;;
public class UserSQLConn {
/**
* @param args
* 实现增删改查
*/
static String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
static String dbUrl="jdbc:sqlserver://localhost:1433;DatabaseName=mydba";
static String us="admin";
static String pw="master";
//连接数据库构造构造方法
public static Connection getConn(String dbDriver,String dbUrl,String us,String pw){
Connection conn=null;
try {
Class.forName(dbDriver);
conn=DriverManager.getConnection(dbUrl,us,pw);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException e1){
e1.printStackTrace();
}
return conn;
}
//创建表
public void dbCreate() throws SQLException {
Connection conn = null;
Statement stat = null;
conn = getConn(driverName, dbUrl, us, pw);
stat = conn.createStatement();
stat
.executeUpdate("create table UserInfo" +
"(userId int," +
"userName varchar(20)," +
"userAddress varchar(20)," +
"userAge int check(userAge between 0 and 150)," +
"userSex varchar(20) default 'M' check(userSex='M' or userSex='W')" +
")");
}
//向表中添加数据
public void addUser(ArrayList<UserInfo> ls) throws SQLException{
Connection conn=getConn(driverName, dbUrl, us, pw);
String insertSql="insert into UserInfo values(?,?,?,?,?);";
PreparedStatement psta=conn.prepareStatement(insertSql);
Iterator<UserInfo> it=ls.iterator();
while(it.hasNext()){
UserInfo uf=it.next();
//设置表字段值
psta.setInt(1, uf.getUserId());
psta.setString(2, uf.getUserName());
psta.setString(3, uf.getUserAddress());
psta.setInt(4, uf.getUserAge());
psta.setString(5, uf.getUserSex());
//往数据库中增加一批数据
psta.addBatch();
}
psta.executeBatch();
psta.close();
conn.close();
}
//查询表select
public void ddlSelect() throws SQLException{
Connection conn=getConn(driverName, dbUrl, us, pw);
Statement sta=conn.createStatement();
ResultSet rs=sta.executeQuery("select * from UserInfo");
while(rs.next()){
int id=rs.getInt("userId");
String name=rs.getString("userName");
String addres=rs.getString("userAddress");
int age=rs.getInt("userAge");
String sex=rs.getString("userSex");
System.out.println(id+"\t"+name+"\t"+addres+"\t"+age+"\t"+sex);
}
}
//删除数据方法
public void ddlDel(int index)throws SQLException{
String ddlDelsql="delete from UserInfo where userId="+index;
Connection conn=getConn(driverName, dbUrl, us, pw);
Statement sta=conn.createStatement();
sta.executeUpdate(ddlDelsql);
sta.close();
conn.close();
}
//修改方法
public void ddlUpdate(String name,
String Address,int age,String sex,int id)throws SQLException{
String ddlUpSql="update UserInfo set userName=?,userAddress=?,userAge=?,userSex=? where userId=?";
Connection conn=getConn(driverName, dbUrl, us, pw);
PreparedStatement psta=conn.prepareStatement(ddlUpSql);
psta.setString(1, name);
psta.setString(2, Address);
psta.setInt(3, age);
psta.setString(4, sex);
psta.setInt(5, id);
psta.addBatch();
psta.executeBatch();
psta.close();
conn.close();
}
public static void main(String[] args) throws SQLException {
//new UserSQLConn().dbCreate();
// UserInfo ufa = new UserInfo(1,"yanther","you kown",30,"M");
// UserInfo ufb = new UserInfo(2,"yang","123678",3,"M");
// UserInfo ufc = new UserInfo(3,"xzg","I dont kown",23,"M");
// UserInfo ufd = new UserInfo(4,"naruto","muye",18,"M");
// ArrayList<UserInfo> arr=new ArrayList<UserInfo>();
// arr.add(ufa);
// arr.add(ufb);
// arr.add(ufc);
// arr.add(ufd);
// new UserSQLConn().addUser(arr);
//new UserSQLConn().ddlDel(3);
// new UserSQLConn().ddlUpdate("name", "kof", 12, "M", 4);
// new UserSQLConn().ddlSelect();
}
}
10 个解决方案
#1
谢谢分享,最近正准备学习这个课题。
#2
这娃不错嘛。。
#3
再重构下,抽象粒度还不够,可以参考sqlhelper.cs
#4
因为只是对userinfo进行了CRUD,如何扩展对主从表进行CRUD,如何不拘于某张表进行CRUD操作。。。当然对于初学SQL的童鞋来讲已经不错了哦
#5
不错 标记一下
#6
[img=https://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/monkey/19.gif]顶起啊各位[/img]
#7
#8
非常好,对我这种初学者来讲我感觉很不错,楼主可以推荐一些关于SqlServer2008学习方面的书籍吗?
#9
霸气~~~好用~~~感谢楼主呀
#10
马克,对初学者来说真的不错哦。楼主加油
#1
谢谢分享,最近正准备学习这个课题。
#2
这娃不错嘛。。
#3
再重构下,抽象粒度还不够,可以参考sqlhelper.cs
#4
因为只是对userinfo进行了CRUD,如何扩展对主从表进行CRUD,如何不拘于某张表进行CRUD操作。。。当然对于初学SQL的童鞋来讲已经不错了哦
#5
不错 标记一下
#6
[img=https://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/monkey/19.gif]顶起啊各位[/img]
#7
#8
非常好,对我这种初学者来讲我感觉很不错,楼主可以推荐一些关于SqlServer2008学习方面的书籍吗?
#9
霸气~~~好用~~~感谢楼主呀
#10
马克,对初学者来说真的不错哦。楼主加油