2007-9-23 18:31
关注者
JDBC一些技巧
[b][size=9pt][font=Times New Roman]package[/font][/size][/b][size=9pt][font=Times New Roman] com.softeem.jdbc.p1;
[b]import[/b] java.sql.Connection;
[b]import[/b] java.sql.DriverManager;
[b]import[/b] java.sql.PreparedStatement;
[b]import[/b] java.sql.ResultSet;
[b]import[/b] java.sql.SQLException;
[b]import[/b] java.sql.Statement;
[b]public[/b] [b]class[/b] CreateTable {
[b]private[/b] [b]static[/b] [b]final[/b] String [i]DRIVER_NAME[/i] = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
[b]private[/b] [b]static[/b] [b]final[/b] String [i]URL[/i] = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=123;DatabaseName=basicInfo";
[b]public[/b] [b]void[/b] update([b]int[/b] id, String booName, [b]float[/b] bookprice, String auto) {
[b]try[/b] {
Class.[i]forName[/i]("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn = DriverManager
.[i]getConnection[/i]("jdbc:microsoft:sqlserver://localhost:1433;user=sa;passWord=123;DatabaseName=basicInfo");
String update = "Update books set bookName=?,bookprice=?,bookauthor=? where bookId=?";
PreparedStatement pstm = conn.prepareStatement(update);
pstm.setString(1, booName);
pstm.setFloat(2, bookprice);
pstm.setString(3, auto);
pstm.setInt(4, id);
pstm.executeUpdate();
pstm.close();
conn.close();
} [b]catch[/b] (ClassNotFoundException e) {
// [b]TODO[/b] Auto-generated catch block
e.printStackTrace();
} [b]catch[/b] (SQLException e) {
// [b]TODO[/b] Auto-generated catch block
e.printStackTrace();
}
}
[b]public[/b] [b]void[/b] selectId([b]int[/b] id) {
[b]try[/b] {
Class.[i]forName[/i]("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn = DriverManager
.[i]getConnection[/i]("jdbc:microsoft:sqlserver://localhost:1433;user=sa;password=123;DatabaseName=basicInfo");
String sql = "select * from books where bookId=?";
PreparedStatement pstm = conn.prepareStatement(sql);
pstm.setInt(1, id);
ResultSet sr = pstm.executeQuery();
[b]while[/b] (sr.next()) {
[b]int[/b] i = sr.getInt("bookId");
String name = sr.getString("bookName");
[b]int[/b] price = sr.getInt("bookprice");
String auto = sr.getString("bookauthor");
System.[i]out[/i].printf("%8d\t%8s\t%8d\t%8s\r\n", i, name, price,
auto);
}
} [b]catch[/b] (ClassNotFoundException e) {
// [b]TODO[/b] Auto-generated catch block
e.printStackTrace();
} [b]catch[/b] (SQLException e) {
// [b]TODO[/b] Auto-generated catch block
e.printStackTrace();
}
}
[b]public[/b] [b]void[/b] select() {
[b]try[/b] {
Class.[i]forName[/i]("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn = DriverManager
.[i]getConnection[/i]("jdbc:microsoft:sqlserver://localhost:1433;user=sa;password=123;DatabaseName=basicInfo");
String select = "select * from books";
PreparedStatement ptsm = conn.prepareStatement(select);
ResultSet rs = ptsm.executeQuery();// [/font][/size][font=宋体][size=9pt]记录器[/size][/font][size=9pt][font=Times New Roman]
[b]while[/b] (rs.next()) {
[b]int[/b] id = rs.getInt("bookId");
String name = rs.getString("bookName");
[b]int[/b] price = rs.getInt("bookprice");
String auto = rs.getString("bookauthor");
System.[i]out[/i].printf("%8d\t%8s\t%8d\t%8s\r\n", id, name, price,
auto);
}
} [b]catch[/b] (ClassNotFoundException e) {
e.printStackTrace();
} [b]catch[/b] (SQLException e) {
// [b]TODO[/b] Auto-generated catch block
e.printStackTrace();
}
}
[b]public[/b] [b]void[/b] newinsert(String bookname, [b]float[/b] price, String auto) {
[b]try[/b] {
Class.[i]forName[/i]("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn = DriverManager
.[i]getConnection[/i]("jdbc:microsoft:sqlserver://localhost:1433;user=sa;password=123;DatabaseName=basicInfo");
String sql = "insert into books values(?,?,?)";
PreparedStatement ptm = conn.prepareStatement(sql);
ptm.setString(1, bookname);
ptm.setFloat(2, price);
ptm.setString(3, auto);
ptm.executeUpdate();
ptm.close();
conn.close();
} [b]catch[/b] (ClassNotFoundException e) {
e.printStackTrace();
} [b]catch[/b] (SQLException e) {
// [b]TODO[/b] Auto-generated catch block
e.printStackTrace();
}
}
[b]public[/b] [b]void[/b] insert(String booName, [b]double[/b] bookprice, String auto) { // jdk1.1[/font][/size][font=宋体][size=9pt]前的JDBC方法[/size][/font][size=9pt][font=Times New Roman]
[b]try[/b] {
Class.[i]forName[/i]([i]DRIVER_NAME[/i]);
Connection conn = DriverManager.[i]getConnection[/i]([i]URL[/i]);
Statement stmt = conn.createStatement();
StringBuilder st = [b]new[/b] StringBuilder();
st.append("INSERT INTO books VALUES('").append(booName)
.append("',").append(bookprice).append(",'").append(auto)
.append("')");
stmt.executeUpdate(st.toString());
stmt.close();
conn.close();
} [b]catch[/b] (ClassNotFoundException e) {
// [b]TODO[/b] Auto-generated catch block
e.printStackTrace();
} [b]catch[/b] (SQLException e) {
// [b]TODO[/b] Auto-generated catch block
e.printStackTrace();
}
}
[b]public[/b] [b]void[/b] newcreate() {
[b]try[/b] {
Class.[i]forName[/i]("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn = DriverManager
.[i]getConnection[/i]("jdbc:microsoft:sqlserver://localhost:1433;User=sa;password=123;databaseName=basicInfo");
String sql = "create table books(bookId int primary key identity(1,1) not null, bookName varchar(20), bookprice float,bookauthor varchar(20))";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.executeUpdate();
stmt.close();
conn.close();
} [b]catch[/b] (ClassNotFoundException e) {
// [b]TODO[/b] Auto-generated catch block
e.printStackTrace();
} [b]catch[/b] (SQLException e) {
// [b]TODO[/b] Auto-generated catch block
e.printStackTrace();
}
}
[b]public[/b] [b]void[/b] create() {
[b]try[/b] {
// id int primary key identity(1,1[/font][/size][font=宋体][size=9pt])[/size][/font][size=9pt][font=Times New Roman] //[/font][/size][font=宋体][size=9pt]每次自增长加[/size][/font][size=9pt][font=Times New Roman]1
Class.[i]forName[/i]([i]DRIVER_NAME[/i]);// [/font][/size][font=宋体][size=9pt]装载[/size][/font][size=9pt][font=Times New Roman]javaDriver
Connection conn = DriverManager.[i]getConnection[/i]([i]URL[/i]);// [/font][/size][font=宋体][size=9pt]建立连接[/size][/font][size=9pt][font=Times New Roman]
// Oracle URL Connection conn =
// DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:scs",username,password);
Statement stmt = conn.createStatement(); // [/font][/size][font=宋体][size=9pt]创建环境[/size][/font][size=9pt][font=Times New Roman]
StringBuilder sb = [b]new[/b] StringBuilder(); // [/font][/size][font=宋体][size=9pt]执行查询返回结果集[/size][/font][size=9pt][font=Times New Roman]
// StringBuilder sb = new StringBuilder(); sb.append("[/font][/size][font=宋体][size=9pt]追加查询或建立的[/size][/font][size=9pt][font=Times New Roman]")
sb.append("create table books(").append(
"book_Id int primary key identity(1,1),").append(
"book_Name varchar(20),").append("book_price float,")
.append("bookauthor varchar(50))");
stmt.executeUpdate(sb.toString()); // [/font][/size][font=宋体][size=9pt]修改[/size][/font][size=9pt][font=Times New Roman] [/font][/size][font=宋体][size=9pt]创建[/size][/font][size=9pt][font=Times New Roman] [/font][/size][font=宋体][size=9pt]都使用它[/size][/font][size=9pt][font=Times New Roman]
// stmt.executeQuery(sb.toString()); [/font][/size][font=宋体][size=9pt]通过它执行[/size][/font][size=9pt][font=Times New Roman] [/font][/size][font=宋体][size=9pt]查询[/size][/font][size=9pt][font=Times New Roman]
stmt.close();
conn.close();
} [b]catch[/b] (ClassNotFoundException e) {
// [b]TODO[/b] Auto-generated catch block
e.printStackTrace();
} [b]catch[/b] (SQLException e) {
// [b]TODO[/b] Auto-generated catch block
e.printStackTrace();
}
} [/font][/size]
[size=9pt][font=Times New Roman][/font][/size]
[size=3][font=Times New Roman][/font][/size]
2007-9-23 18:32
关注者
public void delete(int id) {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn = DriverManager
.getConnection("jdbc:microsoft:sqlserver://localhost:1433;user=sa;password=123;DatabaseName=basicInfo");
String sqldelete = "DELETE FROM BOOKS WHERE bookId=?";
PreparedStatement pstm = conn.prepareStatement(sqldelete);
pstm.setInt(1, id);
pstm.executeUpdate();
pstm.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void deleteAll() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn = DriverManager
.getConnection("jdbc:microsoft:sqlserver://localhost:1433;User=sa;passWord=123;DatabaseName=basicInfo");
String delete = "delete from books";
PreparedStatement pstm = conn.prepareStatement(delete);
pstm.executeUpdate();
pstm.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2007-9-23 18:32
关注者
public void drop(String books) {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn = DriverManager
.getConnection("jdbc:microsoft:sqlserver://localhost:1433;User=sa;passWord=123;DatabaseName=basicInfo");
String drop = "drop table " + books;
PreparedStatement pstm = conn.prepareStatement(drop);
pstm.executeUpdate();
pstm.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
CreateTable create = new CreateTable();
// create.create(); //statement
// create.newcreate(); //preparedstatement 建表
// preparedstatement //新建一个表
// create.insert("java", 12.4, "liubowen");//statement //以后不使用
// create.newinsert(".NET", 2500.2f,"ting");// preparedstatement 以后使用
// create.delete(1); //根据ID
// create.update(3, ".Net", 76, "liubowen"); //修改
// create.select(); //查询
// create.selectId(9);//根据ID查询一条记录
// create.deleteAll(); //删除所有记录
// create.drop("books");
}
}