时间:2015-07-27 07:28 来源: 我爱IT技术网 编辑:52微风
2、java数据库访问类和接口详见《java数据库访问类和接口》
3、java数据库查询操作详见《Java数据库查询详解》
4、java数据库增加记录源代码实例如下:
1)、java MySQL数据库增加记录源码
package com;
import java.sql.*;
/** *//**
* @author Administrator
*/
public class Lianjie {
private Connection con = null ;
private Statement stmt = null ;
private String url = "jdbc:mysql://localhost/test ";
private String user = "root ";
private String pwd = "0429 ";
/** *//** Creates a new instance of Operation */
public Lianjie() {
init();
}
/** *//** init */
private void init(){
try {
Class.forName("com.mysql.jdbc.Driver ").newInstance();
con = DriverManager.getConnection(url ,user ,pwd );
stmt = con .createStatement();
} catch (Exception e){
// your installation of JDBC Driver Failed
e.printStackTrace();
}
}
/** *//**
* TODO 增加一条记录
* @param sn 学生名字
* @param ss 学生性别
* @param sa 学生年龄
* @param so 学生专业
* @return void
*/
public void add(String sn,String ss,String sa,String so){
String sql2 = "insert into student value ('"
+sn+"',' "+ss+"',' "+sa+"',` "+so+"` ); ";
try {
stmt.execute(sql2);
}catch (SQLException e){
e.printStackTrace();
}
}2)、java实现增,删,改,查,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(ArrayListls) throws SQLException{
Connection conn=getConn(driverName, dbUrl, us, pw);
String insertSql="insert into UserInfo values(?,?,?,?,?);";
PreparedStatement psta=conn.prepareStatement(insertSql);
Iteratorit=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");
// ArrayListarr=new ArrayList();
// 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();
}
}- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
