Home MySQL、SQLServer、Oracle三种数据库的JDBC连接方式
Post
Cancel

MySQL、SQLServer、Oracle三种数据库的JDBC连接方式

MySQL

1
2
3
4
5
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?characterEncoding=UTF8";
Connection connection = DriverManager.getConnection(url, username, password);
Statement stmt = connection.createStatement();
Result result = stmt.executeUpdate(sql);

SQLServer

1
2
3
4
5
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:microsoft.sqlserver://localhost:1433/test";
Connection connection = DriverManager.getConnection(url, username, password);
Statement stmt = connection.createStatement();
Result result = stmt.executeUpdate(sql);

Oracle

1
2
3
4
5
6
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:test";
Connection connection = DriverManager.getConnection(url, username, password);
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString("param");
Result result = pstmt.executeUpdate();
This post is licensed under CC BY 4.0 by the author.