预备工作:
a.建立java工程,并在工程中建立Source Folder,在资源夹下创建两个包分别为cn.jdbc.test和cn.jdbc.utils
在test包下创建java类LastTest,在utils包下创建java类JDBCUtils.
b.连接数据库的jar包(本机安装的是sql server 2000,需要的jar包msbase.jar mssqlserver.jar msutil.jar)
1.创建JDBCUtils类代码
package cn.jdbc.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCUtils {
private static final String DRIVER="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=news";
private static final String USER="sa";
private static final String PASSWORD="water";
public static Connection con=null;
static PreparedStatement pst=null;
static ResultSet rs=null;
static {
try {
Class.forName(DRIVER);
try {
con=DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static PreparedStatement getpst(String sql){
try {
pst=con.prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pst;
}
public static ResultSet getrs(PreparedStatement p){
try {
rs=p.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
public static int getquery(){
int a=0;
try {
a=pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return a;
}
public static void closeObject(Connection c,PreparedStatement p,ResultSet r){
if(r!=null)
try {
r.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(p!=null)
try {
p.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(c!=null)
try {
c.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void closeObject(Connection c,PreparedStatement p){
if(p!=null)
try {
p.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(c!=null)
try {
c.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.创建实现查询的java代码
package cn.jdbc.test;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import cn.jdbc.utils.*;
public class LastTest {
public static void main(String[] args){
//Connection con1=null;
String sql="select * from users where username=?";
PreparedStatement pst1=null;
ResultSet rs1=null;
pst1=JDBCUtils.getpst(sql);
try {
pst1.setString(1, "飞鸟");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
rs1=JDBCUtils.getrs(pst1);
try {
while(rs1.next()){
String id=rs1.getString(1);
String username=rs1.getString(2);
String password=rs1.getString(3);
String nickname=rs1.getString(4);
System.out.println(id+" "+username+""+password+""+nickname);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtils.closeObject(JDBCUtils.con, pst1,rs1);
}
}
}