package com.example.hbase.test;(改包名)
import java.sql.*;
public class Hbasezuoye {(改类名)
public static void main(String[] args) throws ClassNotFoundException {
Class.forName("com.mysql.cj.jdbc.Driver");
String mysqlURL = "jdbc:mysql://localhost:3306/zuoye?serverTimezone=UTC";
String username ="root";(改数据库用户名)
String password = "123456";(改数据库密码)
String sql = "select * from a_users";
Connection con =null;
Statement stat =null;
Statement ps = null;
ResultSet rs = null;
ResultSetMetaData rsd = null;
try {
con=DriverManager.getConnection(mysqlURL,username,password);
stat=con.createStatement();
ps=con.prepareStatement(sql);
rs=ps.executeQuery(sql);
rsd=rs.getMetaData();
int lie =rsd.getColumnCount();
while (rs.next()){
for (int i=1;i<=lie;i++){
System.out.println(rs.getObject(i)+"\t");
}
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if (ps !=null){
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (con!=null){
try{
con.close();
con=null;
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
JDBD链接Mysql
最新推荐文章于 2023-04-30 14:26:01 发布