A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 赵小豪 中级黑马   /  2014-4-6 20:11  /  1010 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

求Java的JDBC中数据查询与更新的详细例子?

评分

参与人数 1黑马币 +3 收起 理由
zhangbingyuan + 3 很给力!

查看全部评分

3 个回复

倒序浏览
本帖最后由 zhangbingyuan 于 2014-4-6 20:48 编辑

数据查询:package simple; import java.sql.*;     public class T{   
public static void main(String[] args)  {
String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
//SQL数据库引擎
String connectDB="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Mydb";
//数据源
try { Class.forName(JDriver);
//加载数据库引擎,返回给定字符串名的类
}catch(ClassNotFoundException e) {
e.printStackTrace();
System.out.println("加载数据库引擎失败");
System.exit(0);
}     
System.out.println("数据库驱动成功");  
try {
char id = '2';
String user="sa";
String password="sa";
Connection con=DriverManager.getConnection(connectDB,user,password);
//连接数据库对象
System.out.println("连接数据库成功");
Statement stmt=con.createStatement();
//创建SQL命令对象 /*      
  //创建表 System.out.println("开始创建表");
String query="create table TABLE1(ID NCHAR(2),NAME NCHAR(10))";
//创建表SQL语句
stmt.executeUpdate(query);
//执行SQL命令对象
System.out.println("表创建成功");  
//输入数据 System.out.println("开始插入数据");
String a1="INSERT INTO TABLE1 VALUES('1','旭哥')";
//插入数据SQL语句
String a2="INSERT INTO TABLE1 VALUES('2','伟哥')";
String a3="INSERT INTO TABLE1 VALUES('3','张哥')";
stmt.executeUpdate(a1);//执行SQL命令对象 stmt.executeUpdate(a2);  
  stmt.executeUpdate(a3); System.out.println("插入数据成功"); */
//读取数据 System.out.println("开始读取数据");
ResultSet rs=stmt.executeQuery("SELECT * FROM TABLE1 WHERE ID=id");
//返回SQL语句查询结果集(集合)
//循环输出每一条记录
while(rs.next())
      {
     //输出每个字段
System.out.println(rs.getString("ID")+"\t"+rs.getString("NAME"));     
  }
System.out.println("读取完毕");
//关闭连接
stmt.close();
//关闭命令对象连接
con.close();
//关闭数据库连接
} catch(SQLException e) {
e.printStackTrace();
//System.out.println("数据库连接错误");
System.exit(0);
}
}  
}
数据更新:
import java.sql.*;
  public class Example {   
// JDBC driver name and database URL   
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";   
   static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";   
  //  Database credentials  
  static final String USER = "username";   
static final String PASS = "password";      
public static void main(String[] args) {   
Connection conn = null;    Statement stmt = null;    try{      
//STEP 2: Register JDBC driver     
  Class.forName("com.mysql.jdbc.Driver");      
//STEP 3: Open a connection      
System.out.println("Connecting to a selected database...");  
     conn = DriverManager.getConnection(DB_URL, USER, PASS);   
    System.out.println("Connected database successfully...");           
   //STEP 4: Execute a query      
System.out.println("Creating statement...");      
stmt = conn.createStatement();     
  String sql = "UPDATE Registration " +        "SET age = 30 WHERE id in (100, 101)";      
stmt.executeUpdate(sql);      
// Now you can extract all the records      
// to see the updated records      
sql = "SELECT id, first, last, age FROM Registration";     
  ResultSet rs = stmt.executeQuery(sql);     
   while(rs.next()){      
   //Retrieve by column name      
    int id  = rs.getInt("id");        
  int age = rs.getInt("age");         
String first = rs.getString("first");         
String last = rs.getString("last");      
     //Display values         
System.out.print("ID: " + id);      
   System.out.print(", Age: " + age);        
  System.out.print(", First: " + first);         
System.out.println(", Last: " + last);   
   }      
rs.close();    }catch(SQLException se){   
    //Handle errors for JDBC   
   se.printStackTrace();   
}catch(Exception e){   
  //Handle errors for Class.forName     
  e.printStackTrace();   
}finally{  
     //finally block used to close resources     
  try{      
   if(stmt!=null)           
  conn.close();       }catch(SQLException se){  
     }
// do nothing   
   try{      
   if(conn!=null)        
     conn.close();   
   }catch(SQLException se){      
   se.printStackTrace();       }
//end finally try    }
//end try   
System.out.println("Goodbye!"); }
//end main
}

评分

参与人数 1技术分 +1 收起 理由
枫儿 + 1 赞一个!

查看全部评分

回复 使用道具 举报
表示只用过ADO 技术没用JDBC
回复 使用道具 举报
JDBC由一组使用Java语言编写的类和接口组成,可以为多种关系数据库提供统一访问。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马