JAVA链接mysql数据库

199 篇文章 0 订阅
package sjk_1;
import java.sql.*;
public class sjkmysql_1 {
public static void main(String[] args){
Connection con;
String driver = "com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/usb";
String user="root";
String password="root";
try{
Class.forName(driver);
con=DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("—————————————");
System.out.println("姓名"+"\t"+"职称");
Statement statement = con.createStatement();
String sql = "select * from emp";
ResultSet rs = statement.executeQuery(sql);
System.out.println("—————————————");
String job = null;
String id = null;
while(rs.next()){
job = rs.getString("job");
id = rs.getString("ename");
               System.out.println(id + "\t" + job);
           }
           rs.close();
           con.close();
       } catch(ClassNotFoundException e) {   
           System.out.println("Sorry,can`t find the Driver!");   
           e.printStackTrace();   
           } catch(SQLException e) {
           e.printStackTrace();  
           }catch (Exception e) {
           e.printStackTrace();
       }finally{
           System.out.println("数据库数据成功获取!!");
       }
   }


}

image
最新2020整理收集的一些高频面试题(都整理成文档),有很多干货,包含mysql,netty,spring,线程,spring cloud、jvm、源码、算法等详细讲解,也有详细的学习规划图,面试题整理等,需要获取这些内容的朋友请加Q君样:909038429
/./*欢迎加入java交流Q君样:909038429一起吹水聊天

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值