JDBC

                     JDBC驱动

package com.hbtt.oop17;


import java.sql.*;
 
public class JDBCTest {

public static void main(String[] args){
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/myschool";
String user = "root";
String password = "hbtt";
// 1、加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("成功加载驱动");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
// 2、建立连接
conn = DriverManager.getConnection(url,user,password);

Statement stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT id,stuName, password, mobile , email, address FROM student");
System.out.println("ID\t姓名\t密码\t手机号\t\te-mail\t\t住址");
while(rs.next()){
int a = rs.getInt("id");
String b = rs.getString("stuName");
String c = rs.getString("password");
String d = rs.getString("mobile");
String e = rs.getString("email");
String f = rs.getString("address");
System.out.println(a+"\t"+b+"\t"+c+"\t"+d+"\t"+e+"\t"+f);
}

} catch (SQLException e) {
e.printStackTrace();
} finally {
// 4、关闭ResultSet、Statement和数据库连接
try {
if (null != rs) {
rs.close();
}
if (null != pstmt) {
pstmt.close();
}
if (null != conn) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值