1.連接MySQL數據庫
package mysql;
import java.sql.*;
public class ConnectMysql {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/databaseName";
String user = "mysqluser";
String password = "password";
String driverClass = "com.mysql.cj.jdbc.Driver";
Connection connection = null;
Class.forName(driverClass);
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
if (connection != null) {
System.out.println("數據庫連接成功");
} else {
System.out.println("數據庫連接失敗");
connection.close();
}
return connection;
}
public void getResult() throws ClassNotFoundException, SQLException {
Statement statement = getConnection().createStatement();
String sql = "";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println("ID : " +id);
System.out.println("name :" + name);
}
resultSet.close();
statement.close();
getConnection().close();
}
}
2.連接Oracle數據庫
package cn.zhisheng.test.jdbc;
import oracle.jdbc.driver.OracleDriver;
import java.sql.*;
import java.util.Properties;
public class JdbcTest
{
public static void main(String[] args) {
Connection connect = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Driver driver = new OracleDriver();
DriverManager.deregisterDriver(driver);
Properties pro = new Properties();
pro.put("user", "你的oracle數據庫用戶名");
pro.put("password", "用戶名密碼");
connect = driver.connect("jdbc:oracle:thin:@localhost:1521:XE", pro);
System.out.println(connect);
PreparedStatement preState = connect.prepareStatement("select * from tb1_dept where id = ?");
preState.setInt(1, 2);
boolean execute = preState.execute();
System.out.println(execute);
while (resultSet.next())
{
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String city = resultSet.getString("city");
System.out.println(id+" "+name+" "+city);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (resultSet!=null) resultSet.close();
if (statement!=null) statement.close();
if (connect!=null) connect.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
3.連接SqlServer數據庫
package com.connDBTest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class GetConnectionSqlServer {
public void getConnectionSqlServer() {
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String dbURL = "jdbc:sqlserver://localhost:1433;databasename=USCSecondhandMarketDB";
String userName = "sa";
String userPwd = "sa";
Connection dbConn = null;
try {
Class.forName(driverName).newInstance();
} catch (Exception ex) {
System.out.println("驅動加載失敗");
ex.printStackTrace();
}
try {
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("成功連接數據庫!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (dbConn != null)
dbConn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
GetConnectionSqlServer getConn = new GetConnectionSqlServer();
getConn.getConnectionSqlServer();
}
}