JDBC数据库编程

这篇博客介绍了如何使用Java JDBC进行数据库编程,包括加载Driver驱动、建立数据库连接、创建Statement对象和PreparedStatement,执行SQL语句以及从结果集中获取数据。通过示例代码详细展示了数据库连接、查询操作以及数据处理的过程。
摘要由CSDN通过智能技术生成

1.注册加载一个Drvier驱动:

Class.forName("..........");

2.创建数据库连接

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. String url="jdbc:oracle:thin:@172.16.0.6:1521:tangjl";  
  2.   
  3. String user="openlab";  
  4.   
  5. String password="open123";  
  6.   
  7. Connection Con=DriverManager.getConnection(url,user,password);  
Connection  连接是通过 DriverManager 的静态方法 getConnection 方法得到的这个方法的实质时把参数传到实际的 Dirver 中的 connect() 方法中来获得数据库连接 .

Jdbc:oracle:this:(协议)@xxx.xxx.xxx:xxx(ip地址及端口号):xxx(使用数据库名)

Mysqlurljdbc:mysql://localhost:3306/test

3.获得一个statement对象(PreparedStatement)

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. String sql="select * from a_yyy";  
  2. Statement s=con.createStatment();  
  3. PreparedStatement ps=con.prepareStatement(sql);  


4.通过Statement执行sql语句:

Statement接口代表了一个数据库的状态,在向数据库发送相应的SQL语句时,都需要创建Statement接口或者PreparedStatement接口。在具体应用中,Statement主要用于操作不带参数(可以直接运行)的SQL语句,比如删除语句、添加或更新。

Sta.execute():适合任何语句

sta.executeQuery(sql):适合查询语句.返回一个查询结果集.

sta.executeUpdate(sql);适合增删改创建语句,返回值为影响记录的条数

5.获取结果集里面的数据



按照上面的步骤写,我相信写代码的时候思路会是很清晰的,速度也会提高不少。

下面是代码例子:



[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package cn.university.util;  
  2.   
  3. import java.io.IOException;  
  4. import java.sql.Connection;  
  5. import java.sql.DriverManager;  
  6. import java.sql.ResultSet;  
  7. import java.sql.SQLException;  
  8. import java.util.ArrayList;  
  9. import java.util.Iterator;  
  10. import java.util.List;  
  11. import java.util.Properties;  
  12.   
  13. import cn.university.bin.employee;  
  14.   
  15.   
  16. import com.mysql.jdbc.Statement;  
  17.   
  18. public class JdbcUtil {  
  19.        
  20.     public static Connection conn=null;  
  21.     public static Statement st=null;  
  22.     public static ResultSet rs=null;  
  23.     public static Properties config=new Properties();  
  24.       
  25.     static {  
  26.         try {  
  27.             try {  
  28.                 config.load(JdbcUtil.class.getClassLoader().getResourceAsStream("config.porpertise"));  
  29.             } catch (IOException e) {  
  30.                 // TODO Auto-generated catch block  
  31.                 e.printStackTrace();  
  32.             }  
  33.             //1.加载驱动:  
  34.             Class.forName(config.getProperty("driver"));  
  35.               
  36.         } catch (ClassNotFoundException e) {  
  37.             // TODO Auto-generated catch block  
  38.             e.printStackTrace();  
  39.         }  
  40.           
  41.           
  42.     }  
  43.     //返回数据库的链接  
  44.     public static Connection getConnection() throws SQLException{  
  45.         //2.创建链接  
  46.         return DriverManager.getConnection(config.getProperty("url"),   
  47.                 config.getProperty("username"), config.getProperty("password"));  
  48.     }  
  49.       
  50.     public static void main (String args[]) throws Exception{  
  51.           
  52.         conn=getConnection();  
  53.         //3.获取一个Statement的对象:  
  54.         st=(Statement) conn.createStatement();  
  55.       
  56.         //通过Statement操作数据库:  
  57.         rs=st.executeQuery("select * from users");  
  58.           
  59.         List<employee> s=new ArrayList<employee>();  
  60.         //5.从结构集获取数据  
  61.         while(rs.next()){  
  62.             employee e=new employee();  
  63.             e.setId(rs.getString("id"));  
  64.             e.setName(rs.getString("name"));  
  65.             e.setPassword(rs.getString("password"));  
  66.             e.setBirthday(rs.getDate("birthday"));  
  67.             e.setSalary(rs.getDouble("salary"));  
  68.             e.setDepartment_id(rs.getString("department"));  
  69.               
  70.               
  71.             s.add(e);  
  72.           
  73. //          System.out.println(rs.getInt("id")+"\t"+rs.getString("name")+"\t"+rs.getString("password")+"\t"+rs.getDate("birthday")+"\t"+rs.getString("email"));  
  74.         }  
  75.         System.out.println("ID\t姓名\t密   码 \t出 生 日 期\t工资\t部门");  
  76.         Iterator<employee> it=s.iterator();  
  77.         while (it.hasNext()){  
  78.             employee e=it.next();  
  79.             System.out.println  
  80.                 (e.getId()+"\t"+e.getName()+"\t"+  
  81.                                         e.getPassword()+"\t"+e.getBirthday()+"\t"+e.getSalary()+"\t"+e.getDepartment_id());  
  82.               
  83.               
  84.         }  
  85.           
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值