Java連接sqlserver
import Java.sql.*;public class TestDB {
public static void main(String[] args) {
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String dbURL = "jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=pubs";
String userName = "sa";
String userPwd = "";
Connection dbConntry {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!");
}
Catch (Exception e) {
e.printStackTrace();
}
}
}
Java連接MySQL
import Java.sql.*;public class SampleIntro
{
public static void main(String[] args)
{
try
{
Connection conn;
Statement stmt;
ResultSet res;
//加載Connector/J驅動
//這一句也可寫為:Class.forName("com.MySQL.jdbc.Driver");
Class.forName("com.MySQL.jdbc.Driver").newInstance();
//建立到MySQL的連接
conn = DriverManager.getConnection("jdbc:MySQL://localhost:3306/test",
"root", "guapo");
//執行SQL語句
stmt = conn.createStatement();
res = stmt.executeQuery("select * from pet");
//處理結果集
while (res.next())
{
String name = res.getString("name");
System.out.println(name);
}
res.close(); }
catch (Exception ex)
{
System.out.println("Error : " + ex.toString());
}
}
}
Java連接Oracle
(1) 使用jdbc-odbc橋接:
// 使用本地的jdbc連接串,查詢Oracle數據庫表