package T1; import java.sql.Connection; import java.sql.DriverManager; public class t1 { public static void main(String[] args) { String driverName="com.mysql.jdbc.Driver"; String userName="root"; String userPwd="1234"; String dbName="student"; String url1="jdbc:mysql://localhost:3306/"+dbName; String url2="?user="+userName+"&password="+userPwd; String url3="&useUnicode=true&characterEncoding=UTF-8"; String url=url1+url2+url3; Class.forName(driverName); Connection conn=DriverManager.getConnection(url); } }
时间: 2023-06-08 19:05:38 浏览: 182
JDBC及java.sql包简介
4星 · 用户满意度95%
"&password=" userPwd "&useSSL=false&serverTimezone=UTC"; //拼接连接字符串 try { //1、加载驱动 Class.forName(driverName); //2、获取连接 Connection conn=DriverManager.getConnection(url1+url2); System.out.println("数据库连接成功!"); } catch (Exception e) { e.printStackTrace(); System.out.println("数据库连接失败!"); } }
你希望了解什么关于这段Java代码片段的信息?
阅读全文