JAVA链接mysql数据库

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一起吹水聊天

你可能感兴趣的:(编程,java,java)