QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1026|回复: 0

关于JDBC的,刚弄的

[复制链接]
发表于 2003-11-19 23:38:37 | 显示全部楼层 |阅读模式
建表[code:1]
import java.sql.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class JdbcDemo {
  public static void main(String[] args) {
    int id ;
    String title ;
    String author;
    String query = "SELECT id, title,author FROM book_info";
    try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //load the JDBC driver
      Connection con = DriverManager.getConnection("jdbc:odbc:SQLTEST");
      //get a connection
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery(query);//execute query
     while (rs.next()){//parse the results
       id= rs.getInt("Id");
       title = rs.getString("Title");
       author = rs.getString("author");
       System.out.println("id:"+id+"\t"+"title:"+title+"\t"+"author:"+author);
     }
   }
    catch(ClassNotFoundException e){
      e.printStackTrace();
    }
    catch(SQLException e){
      e.printStackTrace();
    }

  }
}
[/code:1]
查询[code:1]
import java.sql.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class JdbcDemo1 {
  public static void main(String[] args) {
    int id ;
    String title ;
    String author;
    String query = "SELECT * FROM book_info WHERE id=3";
    try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //load the JDBC driver
      Connection con = DriverManager.getConnection("jdbc:odbc:SQLTEST");
      //get a connection
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery(query);//execute query
     while (rs.next()){//parse the results
       id= rs.getInt("Id");
       title = rs.getString("Title");
       author = rs.getString("author");
       System.out.println("id:"+id+"\t"+"title:"+title+"\t"+"author:"+author);
     }
     con.close();
    }
    catch(ClassNotFoundException e){
      e.printStackTrace();
    }
    catch(SQLException e){
      e.printStackTrace();
    }

  }
}
[/code:1]
插入[code:1]
import java.awt.event.*;
import java.sql.*;
import sun.jdbc.odbc.JdbcOdbcDriver;

/**
* <p>Title: InsertSelect</p>
* <p>Description:在JDBC中使用INSERT...SELECT </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company:bjut </p>
* @author not attributable
* @version 1.0
*/



public class InsertSelect{
  static String jdbcDriver =  "sun.jdbc.odbc.JdbcOdbcDriver";
  static String dbName = "SQLTEST";
  static String urlRoot= "jdbc:odbc:";

  public InsertSelect(){
    registerDriver();
  }
  public void setDatabaseName(String dbName){
    this.dbName=dbName;
  }
  public void registerDriver(){
    try{
      Class.forName(jdbcDriver);
      DriverManager.registerDriver(new JdbcOdbcDriver());
    }
    catch(ClassNotFoundException e){
      System.err.print(e.getMessage());
    }
    catch(SQLException e){
      System.err.println(e.getMessage());
    }
  }
  public void execute(String SQLCommand){
    String url = urlRoot + dbName;
    try {
      Connection con = DriverManager.getConnection(url);
      Statement stmt = con.createStatement();
      stmt.execute(SQLCommand);
      con.close();
    }
    catch (SQLException e) {
      System.err.println(e.getMessage());
    }
  }
public static void main(String args[]){
    InsertSelect inserter = new InsertSelect();
    String SQLCommand = " INSERT INTO NAMES "+
        "SELECT First_Name,Last_Name FROM CONTACT_INFO "+
        "WHERE Last_Name = 'feng';";
    inserter.execute(SQLCommand);
     }
}[/code:1]
删除[code:1]
import java.awt.event.*;
import java.sql.*;
import sun.jdbc.odbc.JdbcOdbcDriver;

/**
* <p>Title: DataDelete</p>
* <p>Description:在JDBC中使用Delete </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company:bjut </p>
* @author not attributable
* @version 1.0
*/



public class DataDelete{
  static String jdbcDriver =  "sun.jdbc.odbc.JdbcOdbcDriver";
  static String dbName = "SQLTEST";
  static String urlRoot= "jdbc:odbc:";
  private ActionListener exceptionListener = null;

  public DataDelete(){
    registerDriver();
  }
  public void setDatabaseName(String dbName){
    this.dbName=dbName;
  }
  public void registerDriver(){
    try{
      Class.forName(jdbcDriver);
      DriverManager.registerDriver(new JdbcOdbcDriver());
    }
    catch(ClassNotFoundException e){
      System.err.print(e.getMessage());
    }
    catch(SQLException e){
      System.err.println(e.getMessage());
    }
  }
  public void execute(String SQLCommand){
    String url = urlRoot + dbName;
    try {
      Connection con = DriverManager.getConnection(url);
      Statement stmt = con.createStatement();
      stmt.execute(SQLCommand);
      con.close();
    }
    catch (SQLException e) {
      System.err.println(e.getMessage());
    }
  }
public static void main(String args[]){
    DataDelete inserter = new DataDelete();
    String SQLCommand = " DELETE FROM CONTACT_INFO "+
        "WHERE PHONE = '13';";
    inserter.execute(SQLCommand);
     }
}[/code:1]
更新[code:1]
import java.awt.event.*;
import java.sql.*;
import sun.jdbc.odbc.JdbcOdbcDriver;

/**
* <p>Title: DataUpdater</p>
* <p>Description:在JDBC中使用Updater </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company:bjut </p>
* @author not attributable
* @version 1.0
*/



public class DataUpdater{
  static String jdbcDriver =  "sun.jdbc.odbc.JdbcOdbcDriver";
  static String dbName = "SQLTEST";
  static String urlRoot= "jdbc:odbc:";
  private ActionListener exceptionListener = null;

  public DataUpdater(){
    registerDriver();
  }
  public void setDatabaseName(String dbName){
    this.dbName=dbName;
  }
  public void registerDriver(){
    try{
      Class.forName(jdbcDriver);
      DriverManager.registerDriver(new JdbcOdbcDriver());
    }
    catch(ClassNotFoundException e){
      System.err.print(e.getMessage());
    }
    catch(SQLException e){
      System.err.println(e.getMessage());
    }
  }
  public void execute(String SQLCommand){
    String url = urlRoot + dbName;
    try {
      Connection con = DriverManager.getConnection(url);
      Statement stmt = con.createStatement();
      stmt.execute(SQLCommand);
      con.close();
    }
    catch (SQLException e) {
      System.err.println(e.getMessage());
    }
  }
public static void main(String args[]){
    DataUpdater inserter = new DataUpdater();
    String SQLCommand = " UPDATE CONTACT_INFO "+
        "SET EMAIL = '[email protected]'"+
        "WHERE PHONE = '13';";
    inserter.execute(SQLCommand);
     }
}[/code:1]
您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-23 03:16 , Processed in 0.066052 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表