当前位置:Gxlcms > 数据库问题 > JDBC连接数据库

JDBC连接数据库

时间:2021-07-01 10:21:17 帮助过:5人阅读

try{ 2 //加载数据库驱动,注册到驱动管理器 3 Class.forName("com.mysql.jdbc.Driver"); 4 //数据库连接字符串 5 String url = "jdbc:mysql://localhost:3306/mysqltest"; 6 //数据库用户名 7 String userName = "root"; 8 //数据库密码 9 String passWord = "123456"; 10 //创建Connection连接 11 Connection conn = (Connection)DriverManager.getConnection(url,userName,passWord); 12 //判断数据库连接是否为空 13 if(conn != null){ 14 //输出连接信息 15 out.print("数据库连接成功!"); 16 //关闭数据库连接 17 conn.close(); 18 }else{ 19 //输出连接错误信息 20 out.println("数据库连接失败!"); 21 } 22 }catch(ClassNotFoundException e){ 23 e.printStackTrace(); 24 } View Code

技术分享

JDBC连接数据库

标签:trace   pen   驱动   http   image   密码   字符   str   etc   

人气教程排行