Java JDBC建立和Mysql、sql server 2005、sqlite和Oracle数据库的连接

时间:2021-11-15 13:37:23

1.与MySQL数据库的连接

import java.sql.*;
import java.util.*;

public class GetConn {
Connection conn = null;
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver"); // 加载MySQL数据库驱动
System.out.println("数据库驱动加载成功!!");
String url = "jdbc:mysql://localhost:3306/db_database03"; // 定义与连接数据库的url
String user = "root"; // 定义连接数据库的用户名
String passWord = "111"; // 定义连接数据库的密码
conn = DriverManager.getConnection(url, user, passWord); // 连接连接
System.out.println("已成功的与MySQL数据库建立连接!!");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public List getBccd(){
List list = new ArrayList<Bccd>();
conn = getConnection();
try {
Statement statement = conn.createStatement();
String sql = "select * from tb_bccd";
ResultSet rest = statement.executeQuery(sql);
while(rest.next()){
Bccd bccd = new Bccd();
bccd.setId(rest.getInt(1));
bccd.setbName(rest.getString(2));
bccd.setPrice(rest.getFloat(3));
list.add(bccd);
}
} catch (SQLException e) {

e.printStackTrace();
}

return list;
}

}

mysql 数据库jar包:http://download.csdn.net/detail/liaoqianwen123/6783545

2. 与SQl server 2005的连接

import java.sql.*;
public class GetConn {
private Connection conn ; //定义Connection对象
public Connection getConnection(){ //定义连接数据库方法
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //加载数据库驱动
System.out.println("数据库驱动加载成功!");
//定义连接数据库URL
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=master";
String userName = "sa";
String passWord = "";
conn = DriverManager.getConnection(url,userName ,passWord); //获取数据库连接
if(conn != null){
System.out.println("已成功的与SQLServer2005数据库建立连接!");
}
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) {
GetConn getConn = new GetConn();
getConn.getConnection();
}
}

SQl server 2005 jar包:http://download.csdn.net/detail/liaoqianwen123/6783539

3.与Oracle 11g的连接

import java.sql.*;
public class GetConn {
public Connection getConnection() {
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver"); //加载数据库驱动
System.out.println("数据库驱动加载成功!"); //输出的信息
String url = "jdbc:oracle:thin:@localhost:1521:orcl3"; //获取连接URL
String user = "system"; //连接用户名
String password = "aaa"; //连接密码
Connection con = DriverManager.getConnection(url, user, password); //获取数据库连接
if (con != null) {
System.out.println("成功的与Oracle数据库建立连接!!");
}
} catch (Exception e) {
e.printStackTrace();
}
return conn; //返回Connection实例
}
public static void main(String[] args) {
GetConn getConn = new GetConn();
getConn.getConnection();
}
}

Oracle的jar包: http://download.csdn.net/detail/liaoqianwen123/6783531

4.与sqlite的连接

import java.sql.*;

public class SQLiteJDBC
{
public static void main( String args[] )
{
Connection c = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:test.db");
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Opened database successfully");
}
}

sqlite的jar包:http://download.csdn.net/detail/liaoqianwen123/6783549