关于JDBC的,刚弄的
建表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();
}
}
}
查询
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();
}
}
}
插入
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);
}
}
删除
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);
}
}
更新
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);
}
}
页:
[1]