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

时间:2021-11-18 18:39:51
 package it.cast.jdbc;

 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class Base { public static void main(String[] args) throws SQLException {
test();
} static void test() throws SQLException {
// 1.注册驱动
DriverManager.registerDriver(new com.mysql.jdbc.Driver()); // 2.建立连接
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jdbc", "root", "123"); // 3.创建语句
Statement st = conn.createStatement(); // 4.执行语句
ResultSet rs = st.executeQuery("select * from user"); // 5.处理结果
while (rs.next()) {
System.out.println(rs.getObject(1) + "\t" + rs.getObject(2) + "\t"
+ rs.getObject(3)+"\t" + rs.getObject(4));
} //6.释放资源
rs.close();
st.close();
conn.close();
} }

BASE

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