登录
首页 >  数据库 >  MySQL

JAVA链接mysql数据库

来源:SegmentFault

时间:2023-02-16 15:27:49 455浏览 收藏

在数据库实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《JAVA链接mysql数据库》,聊聊MySQL、Java,希望可以帮助到正在努力赚钱的你。

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链接mysql数据库》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于mysql的知识点!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表