首页 > 代码库 > 编写一个简单的jdbc例子程序

编写一个简单的jdbc例子程序

 1 package it.cast.jdbc; 2  3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8  9 public class Base {10 11     public static void main(String[] args) throws SQLException {12          test();13     }14 15     static void test() throws SQLException {16         // 1.注册驱动17         DriverManager.registerDriver(new com.mysql.jdbc.Driver());18 19         // 2.建立连接20         Connection conn = DriverManager.getConnection(21                 "jdbc:mysql://localhost:3306/jdbc", "root", "123");22 23         // 3.创建语句24         Statement st = conn.createStatement();25 26         // 4.执行语句27         ResultSet rs = st.executeQuery("select * from user");28 29         // 5.处理结果30         while (rs.next()) {31             System.out.println(rs.getObject(1) + "\t" + rs.getObject(2) + "\t"32                     + rs.getObject(3)+"\t" + rs.getObject(4));33         }34         35         //6.释放资源36         rs.close();37         st.close();38         conn.close();39     }40 41 }
BASE

导入jar ==>mysql-connector-java-5.1.22-bin.jar