1.提取单条记录
//import java.sql.*;
Connection con=null;
Statement stmt=null;
ResultSet %%6=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:"+%%1;
con=DriverManager.getConnection(url,%%2,%%3);
stmt=con.createStatement();
stmt.executeUpdate(%%4);
%%6=stmt.executeQuery(%%5);
%%7
}catch(Exception e){
e.printStackTrace();
}finally{
try {
try {
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}2.单值比较
//import java.sql.*;
ResultSet rs;
String strurl="jdbc:odbc:%%1";
Connection con=NULL;
Statement stmt=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,%%2,%%3);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(%%2);
rs.next();
int %%3=rs.getInt("%%3");
if(%%3==%%4)
{
%%5
}
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try {
if(rs!=null)
{
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}3.显示表格
//import java.awt.*;
//import javax.swing.*;
//import java.sql.*;
//import javax.swing.table.*;
String[] colHeads=%%4;
Connection con=null;
Statement stmt=null;
ResultSet %%6=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:"+%%1;
con=DriverManager.getConnection(url,%%2,%%3);
stmt=con.createStatement();
%%6=stmt.executeQuery("SELECT count(*) as au_count from "+%%5);
%%6.next();
int iCount=rs.getInt("au_count");
Object[][] data=new Object[iCount][];
int i=0;
%%6=stmt.executeQuery("SELECT * from "+%%5);
while(%%6.next()){
data[i]=new Object[iCount];
data[i][0]=%%6.getString("au_fname");
data[i][1]=%%6.getString("Phone");
data[i][2]=%%6.getString("City");
i++;
}
JTable table=new JTable(data,colHeads);
JScrollPane jsp=new JScrollPane(table);
getContentPane().add(jsp);
}catch(Exception e){
e.printStackTrace();
}
finally{
if(con!=null){
try {
%%6.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}4.操作表格
//import java.awt.*;
//import java.swing.*;
//import java.sql.*;
//import javax.swing.table.*;
JTable table;
Object[][] data;
JButton jb;
JScrollPane jsp;
JComboBox jcb;
JCheckBox jchb;
jcb=new JComboBox();
jchb=new JCheckBox();
jcb.addItem("男");
jcb.addItem("女");
jb = new JButton("dsfasd");
String[] colHeads = { "id", "性别","是否" };
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:ghost";
con = DriverManager.getConnection(url);
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT count(id) as count from AUTHORS");
rs.next();
int iCount = rs.getInt("count");
data = new Object[iCount][];
int i = 0;
rs = stmt.executeQuery("SELECT * from AUTHORS");
while (rs.next()) {
data[i] = new Object[iCount];
data[i][0] = rs.getString("id");
data[i][1] = rs.getString("gender");
data[i][2] = rs.getString("isMember");
i++;
}
table = new JTable();
DefaultTableModel dt = new DefaultTableModel(data, colHeads); //
table.setModel(dt);
jsp = new JScrollPane(table);
getContentPane().add(jsp, BorderLayout.CENTER);
TableColumn tc = table.getColumn("性别");
TableColumn tc2=table.getColumn("是否");
tc.setCellEditor(new DefaultCellEditor(jcb));
tc2.setCellEditor(new DefaultCellEditor(jchb));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null) {
try {
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
this.setSize(300, 300);
this.setLocation(200, 210);
JPanel jp2 = new JPanel();
this.getContentPane().add(jp2, BorderLayout.NORTH);
JPanel jp = new JPanel();
jp2.add(jp);
jp.add(new JTextField(10));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);

解决方案 »

  1.   


    5.数值范围查询
    //import java.sql.*;
    Connection con;
    Statement stmt;
    ResultSet rs;
    int min = Integer.parseInt(jcb1.getSelectedItem().toString());
    int max = Integer.parseInt(jcb2.getSelectedItem().toString());
    String sql = "select count(*) as pro_count from ProPrice where price between "
    + min + " and " + max;
    try {
    con = DriverManager.getConnection(url);
    stmt = con.createStatement();
    rs = stmt.executeQuery(sql);
    rs.next();
    int iCount = rs.getInt("pro_count");
    Object[][] data = new Object[iCount][];
    rs = stmt.executeQuery("select * from ProPrice where price between "
    + min + " and " + max);
    int i = 0;
    while (rs.next()) {
    data[i] = new Object[3];
    data[i][0] = rs.getString("id");
    data[i][1] = rs.getString("name");
    data[i][2] = rs.getString("price");
    i++;
    }
    DefaultTableModel dt = new DefaultTableModel(data, colHeads);
    table.setModel(dt);
    } catch (Exception e1) {
    e1.printStackTrace();
    } finally {
    try {
    rs.close();
    con.close();
    stmt.close();
    } catch (SQLException e1) {
    e1.printStackTrace();
    }
    }6.关闭时关闭连接
    //import java.sql.*;
    addWindowListener(new WindowAdapter{
    public void windowClosing(WindowEvent wevent){
    if(stmt!=null){
    try {
    stmt.close();
    con.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }7.执行命令
    //import java.sql.*;
    Connection conn;
    PreparedStatement pst=null;
    try {
    conn=DriverManager.getConnection(url);
    pst=conn.prepareStatement("Insert Into grade(%%1) Values (?)");
    pst.setInt(1,%%2);
    //pst.setInt(2,%%2);
    pst.addBatch();
    pst.executeBatch();
    } catch (SQLException e){
    e.printStackTrace();
    }
    finally{
    pst.close();
    conn.close();
    }8.Oracle8/8i/9i数据库(thin模式)
    //import java.sql.*;
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); 
    String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID 
    String user="test"; 
    String password="test"; 
    Connection conn= DriverManager.getConnection(url,user,password);  
    Statement stmtNew=conn.createStatement();9.DB2数据库
    //import java.sql.*;
    Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance(); 
    String url="jdbc:db2://localhost:5000/sample"; //sample为你的数据库名 
    String user="admin"; 
    String password=""; 
    Connection conn= DriverManager.getConnection(url,user,password);  
    Statement stmtNew=conn.createStatement();10.Sql Server7.0/2000数据库
    //import java.sql.*;
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); 
    //String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db2"; //7.0、2000
    String url="jdbc:sqlserver://localhost:1433;DatabaseName=db2"; //2005
    //db2为数据库名
    String user="sa"; 
    String password=""; 
    Connection conn= DriverManager.getConnection(url,user,password);  
    Statement stmtNew=conn.createStatement();11.Sybase数据库
    //import java.sql.*;
    Class.forName("com.sybase.jdbc.SybDriver").newInstance(); 
    String url =" jdbc:sybase:Tds:localhost:5007/myDB";//myDB为你的数据库名 
    Properties sysProps = System.getProperties(); 
    SysProps.put("user","userid"); 
    SysProps.put("password","user_password"); 
    Connection conn= DriverManager.getConnection(url, SysProps);  
    Statement stmtNew=conn.createStatement();12.Informix数据库
    //import java.sql.*;
    Class.forName("com.informix.jdbc.IfxDriver").newInstance(); 
    String url = "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver; 
    user=testuser;password=testpassword"; //myDB为数据库名 
    Connection conn= DriverManager.getConnection(url);  
    Statement stmtNew=conn.createStatement();13.MySQL数据库
    //import java.sql.*;
    //Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    Class.forName("com.mysql.jdbc.Driver");
    //String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1";
    String url ="jdbc:mysql://localhost:3306/myDB";
    //myDB为数据库名 
    Connection conn= DriverManager.getConnection(url,"root","root");  
    Statement stmtNew=conn.createStatement();14.PostgreSQL数据库
    //import java.sql.*;
    Class.forName("org.postgresql.Driver").newInstance();
    String url ="jdbc:postgresql://localhost/myDB" //myDB为数据库名
    String user="myuser";
    String password="mypassword";
    Connection conn= DriverManager.getConnection(url,user,password);
    Statement stmtNew=conn.createStatement();15.access数据库直连用ODBC的
    //import java.sql.*;
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ; 
    String url="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb"); 
    Connection conn = DriverManager.getConnection(url,"sa",""); 
    Statement stmtNew=conn.createStatement();16.程序计时
    long time1=System.currentTimeMillis();
    long time2=System.currentTimeMillis();
    long interval=time2-time1;17.延时
    try {
    Thread.sleep(Integer.Parse(%%1));
    } catch(InterruptedException e) {
    e.printStackTrace();
    }18.连接Excel文件
    //import java.sql.*;
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String url = "jdbc:odbc:driver={Microsoft Excel Driver (*.xls)};DBQ=D:\\myDB.xls"; // 不设置数据源
    String user="myuser";
    String password="mypassword";
    Connection conn= DriverManager.getConnection(url,user,password);
    Statement stmtNew=conn.createStatement();
      

  2.   

    其实这些都不是java数据库方面容易出错的地方,主要在连接处,由于连接的jar文件的不断改动,容易出现错误,现在网上的很多driver名字就不正确,都是老版本的