首页 / MYSQL / jdbc 连接mysql数据库
jdbc 连接mysql数据库
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了jdbc 连接mysql数据库,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1667字,纯文字阅读大概需要3分钟。
内容图文
package test_mysql; import java.sql.*; import java.util.Set; public class testjdbc { public static Connection getConnection() throws ClassNotFoundException, SQLException{ String URL="jdbc:mysql://localhost:3306/employees"; //载入驱动程序 Class.forName("com.mysql.jdbc.Driver"); String user="root"; String passwd=""; //默认密码为空 //还有选择数据库,哪个数据 //DriverManager. //Connection con=DriverManager.getConnection(URL, user, passwd); //Connection con=DriverManager.getConnection(url, user, password); String test_url=""; //String my_url="jdbc:mysql://localhost:3306;DatabaseName=employees;User=root;Password="; //test_url+=URL+3306+";"+"DatabaseName=employees;"+"User=root;"+"Password="+""; Connection con=DriverManager.getConnection(URL,user,passwd); return con; } public static void main(String[] args){ String sql="select * from departments"; Statement statement=null; Connection con=null; ResultSet set=null; try { con = getConnection(); //con.set if(con==null) System.out.println("con null"); statement=con.createStatement(); set=statement.executeQuery(sql); //int n=set.getFetchSize(); //System.out.println(n); while(set.next()){ System.out.println(set.getString(1)+" "+set.getString(2)); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(con!=null) try { set.close(); statement.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
版权声明:本文博客原创文章,博客,未经同意,不得转载。
jdbc 连接mysql数据库
标签:
本文系统来源:http://www.cnblogs.com/gcczhongduan/p/4631196.html
内容总结
以上是互联网集市为您收集整理的jdbc 连接mysql数据库全部内容,希望文章能够帮你解决jdbc 连接mysql数据库所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。