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
编写一个简单的jdbc例子程序,布布扣,bubuko.com
时间: 2024-10-05 08:42:43