JDBC 连接数据库

来源:互联网 发布:ug8.5 编程教程pdf 编辑:程序博客网 时间:2024/06/01 21:25

1、链接Sqlserver2000 
驱动类 
com.microsoft.jdbc.sqlserver.SQLServerDriver 
连接字符串 
jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs 
(pubs为数据库名,localhost为主机地址,若数据库在远程则为IP地址) 

 

2、链接mysql 
驱动类 
com.mysql.jdbc.Driver 
连接字符串 
jdbc:mysql://localhost:3306/pubs 
(pubs为数据库名,localhost为主机地址,若数据库在远程则为IP地址) 


3、jdbc-odbc桥链接 
驱动类 
sun.jdbc.odbc.JdbcOdbcDriver 
连接字符串 
jdbc:odbc:dsnname 
(dsnname为odbc数据源的名称) 


4、链接Oracle 
驱动类: 
         oracle.jdbc.driver.OracleDriver 
链接字符串 
         jdbc:oracle:thin:@localhost:1521:ora9 
        (localhost为主机地址,若数据库,在远程则为IP地址其中“1521”为端口,“ora9”为sid ) 



5、连接DB2 
驱动类: 
        COM.ibm.db2.jdbc.app.DB2Driver 
连接字符串: 
       jdbc:db2://192.168.1.10/50000:rdb 
         (这里192.168.1.10为数据库所在服务器IP地址,而50000为DB2连接服务的端口号) 

六java代码

把上面的这些都做好了,接下来就是写代码~~

以下是我的代码:

package com.sql; 

import java.sql.*; 

public class Test2 

public static void main(String[] args)  

String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL

数据库引擎

 

String connectDB="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=pro";//

数据源

注意IP地址和端口号,数据库名字!!!

try 

Class.forName(JDriver);//

加载数据库引擎,返回给定字符串名的类

 

}catch(ClassNotFoundException e) 

//e.printStackTrace(); 

System.out.println("

加载数据库引擎失败

"); 

System.exit(0); 

}  

System.out.println("

数据库驱动成功

"); 

try 

String user="abc";//

你自己创建的用户名字和密码!!!!!!!!!!!!

 

String password="000000"; 

Connection 

con=DriverManager.getConnection(connectDB,user,password);//

连接数据库对象

 

System.out.println("

连接数据库成功

"); 

Statement stmt=con.createStatement();//

创建SQL命令对象

//创建表

 System.out.println("查询"); 

System.out.println("

开始读取数据

"); 

ResultSet rs=stmt.executeQuery("SELECT * FROM 学生信息 where age=19");

//返回SQL语句查询结果集(集合) 

//循环输出每一条记录

while(rs.next()) 

//输出每个字段

System.out.println(rs.getString("stuId")+"\t"+rs.getString("NAME")); 

System.out.println("

读取完毕

"); 

//关闭连接

stmt.close();//关闭命令对象连接

con.close();//关闭数据库连接

} catch(SQLException e) 

e.printStackTrace(); 

//System.out.println("

数据库连接错误

"); 

System.exit(0); 

}



 

 

 

 java

代码

 

把上面的这些都做好了,接下来就是写代码咯

~~

以下是我的代码

 

package com.sql; 

import java.sql.*; 

public class Test2 

public static void main(String[] args)  

String 

JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL

据库引擎

 

String 

connectDB="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=pro";//

数据源

注意

IP

地址和端口号,数据库名字!!!

 

try 

Class.forName(JDriver);//

加载数据库引擎,返回给定字符串名的类

 

}catch(ClassNotFoundException e) 

//e.printStackTrace(); 

System.out.println("

加载数据库引擎失败

"); 

System.exit(0); 

}  

System.out.println("

数据库驱动成功

"); 

try 

String user="abc";//

你自己创建的用户名字和密码!!!!!!!!!!!!

 

String password="000000"; 

Connection 

con=DriverManager.getConnection(connectDB,user,password);//

连接数据库

对象

 

System.out.println("

连接数据库成功

"); 

Statement stmt=con.createStatement();//

创建

SQL

命令对象

 

//

创建表

 

System.out.println("

查询

"); 

System.out.println("

开始读取数据

"); 

ResultSet rs=stmt.executeQuery("SELECT * FROM 

学生信息

 where 

age=19");//

返回

SQL

语句查询结果集

(

集合

//

循环输出每一条记录

 

while(rs.next()) 

//

输出每个字段

 

System.out.println(rs.getString("stuId")+"\t"+rs.getString("NAME")); 

System.out.println("

读取完毕

"); 

//

关闭连接

 

stmt.close();//

关闭命令对象连接

 

con.close();//

关闭数据库连接

 

catch(SQLException e) 

e.printStackTrace(); 

//System.out.println("

数据库连接错误

"); 

System.exit(0); 

}

0 0
原创粉丝点击