當前位置:首頁 » 操作系統 » jdbc資料庫連接的代碼

jdbc資料庫連接的代碼

發布時間: 2022-08-22 11:23:58

⑴ jdbcl連接資料庫的代碼

try {
//load the jdbc driver ;

//DRIVER URL自己寫
/*
*jdbc:mysql://<hostname>[<:3306>]/<dbname>
*jdbc:sqlserver://<server_name>:<port>[;databaseName=<dbname>]
*jdbc:derby://<server_name>:<port>/<databaseName>[;create=true]
*jdbc:postgresql:[<//host>[:<5432>/]]<database>
*/
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
String url =" jdbc:oracle:thin:@<server>[:<1521>]:<database_name>"
//這個自己寫
String userName =""
//這個自己寫
String userPsw = ""
conn = DriverManager.getConnection(url, userName, userPsw);
//set if autoCommit
conn.setAutoCommit(false);
} catch (Exception ex) {
throw new Exception("connDB Init error." + ex.getMessage());
}

⑵ 求一javaJDBC連接MYSQL資料庫寫法講解

連接代碼如下:
public static void main(String[] args){

// 驅動程序名
String driver = "com.mysql.jdbc.Driver";

// URL指向要訪問的資料庫名scutcs
String url = "jdbc:mysql://127.0.0.1:3306/scutcs";

// MySQL配置時的用戶名
String user = "root";

// MySQL配置時的密碼
String password = "root";

try {
// 載入驅動程序
Class.forName(driver);

// 連續資料庫
Connection conn = DriverManager.getConnection(url, user, password);

if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");

// statement用來執行SQL語句
Statement statement = conn.createStatement();

// 要執行的SQL語句
String sql = "select * from student";

// 結果集
ResultSet rs = statement.executeQuery(sql);

System.out.println("-----------------");
System.out.println("執行結果如下所示:");
System.out.println("-----------------");
System.out.println(" 學號" + "\t" + " 姓名");
System.out.println("-----------------");

String name = null;

while(rs.next()) {

⑶ 請寫出一段JDBC訪問Oracle資料庫的代碼

實現思路:就是通過ojdbc.jar中提供的方法,直接連接資料庫即可,固定代碼寫法如下:

importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.Statement;

publicclassJDBCTest{
publicstaticvoidmain(String[]args)throwsException{
//1.載入驅動
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//Class.forName("com.mysql.jdbc.Driver");
Class.forName("oracle.jdbc.driver.OracleDriver");

//2.創建資料庫連接對象
//Connectionconn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=db","sa","sqlpass");
//Connectionconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF-8","root","123456");
Connectionconn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","Oracle123");

//3.創建資料庫命令執行對象
Statementstmt=conn.createStatement();
// PreparedStatementps=conn.prepareStatement("select*fromt_user");

//4.執行資料庫命令
ResultSetrs=stmt.executeQuery("select*fromt_user");
// ResultSetrs=ps.executeQuery();

//5.處理執行結果
while(rs.next()){
intid=rs.getInt("id");
Stringusername=rs.getString("username");
Stringpassword=rs.getString("password");
System.out.println(id+" "+username+" "+password);
}

//6.釋放資料庫資源
if(rs!=null){
rs.close();
}
// if(ps!=null){
// ps.close();
// }
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}
}

⑷ 求用jdbc 連接資料庫的java代碼(只連接部分即可)

public Connection getConnection(){//事先導入驅動
try{
Class.forname("sun.jdbc.odbc.jdbcodbcDriver");//載入驅動
System.out.println("驅動載入完畢");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
try{
Connecion con=DriverManager.getConnecion("jdbc:odbc:資料庫名成","用戶名","密碼");//創建連接對象,即已經連接到資料庫
system.out.println("連接成功!");
}
catch(exception e){
e.printStackTrace();
}
}

⑸ 分別寫出jdbc連oracle和mysql的主要代碼

JDBC連接不同資料庫的寫法如下:

1、Oracle8/8i/9i資料庫(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl為資料庫的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);

2、SQL Server2005及以上版本資料庫
Class.forName("com.microsoft.sqlserver.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb為資料庫
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);

3、MySQL資料庫
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost/myDB?
user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//myDB為資料庫名
Connection conn= DriverManager.getConnection(url);

4、DB2資料庫
Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample"; //sample為你的資料庫名
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);

5、Sybase資料庫
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/myDB";//myDB為你的資料庫名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);

6、Informix資料庫
Class.forName("com.informix.jdbc.IfxDriver").newInstance();
String url = "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword"; //myDB為資料庫名
Connection conn= DriverManager.getConnection(url);
7、PostgreSQL資料庫
Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/myDB" //myDB為資料庫名
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);

8、access資料庫直連用ODBC的

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
String url="jdbc:odbc:Driver={MicroSoft Access Driver
(*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb");
Connection conn = DriverManager.getConnection(url,"","");
Statement stmtNew=conn.createStatement() ;

熱點內容
android相機閃光燈 發布:2025-05-16 14:35:49 瀏覽:258
伺服器無法通過ip訪問 發布:2025-05-16 14:26:13 瀏覽:540
網吧u盤拒絕訪問 發布:2025-05-16 14:13:50 瀏覽:260
無線網檢查網路配置是怎麼回事 發布:2025-05-16 14:04:03 瀏覽:220
網路爬蟲python代碼 發布:2025-05-16 14:03:26 瀏覽:516
汽車小組件怎麼弄到安卓桌面 發布:2025-05-16 13:51:12 瀏覽:220
linuxg編譯器下載 發布:2025-05-16 13:50:58 瀏覽:776
centosc編譯器 發布:2025-05-16 13:50:17 瀏覽:948
安卓手機如何變換桌面 發布:2025-05-16 13:39:33 瀏覽:515
sql存儲過程命令 發布:2025-05-16 13:17:54 瀏覽:146