Eclipse中JDBC各数据库怎样连接
3个回答
2013-08-16
展开全部
MySQL:
String Driver="com.mysql.jdbc.Driver"; //驱动程序
String URL="jdbc:mysql://localhost:3306/db_name"?useUnicode=true&characterEncoding=UTF-8; //连接的URL,db_name为数据库名,注意修改编码类型
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).new Instance();
Connection con=DriverManager.getConnection(URL,Username,Password);
Microsoft SQL Server 2.0驱动(3个jar的那个):
String Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; //连接SQL数据库的方法
String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name"; //db_name为数据库名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).new Instance(); //加载数据可驱动
Connection con=DriverManager.getConnection(URL,UserName,Password); //
Microsoft SQL Server 3.0驱动(1个jar的那个): // 老紫竹完善
String Driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; //连接SQL数据库的方法
String URL="jdbc:sqlserver://localhost:1433;DatabaseName=db_name"; //db_name为数据库名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).new Instance(); //加载数据可驱动
Connection con=DriverManager.getConnection(URL,UserName,Password); //
Sysbase:
String Driver="com.sybase.jdbc.SybDriver"; //驱动程序
String URL="jdbc:Sysbase://localhost:5007/db_name"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
Oracle(用thin模式):
String Driver="oracle.jdbc.driver.OracleDriver"; //连接数据库的方法
String URL="jdbc:oracle:thin:@loaclhost:1521:orcl"; //orcl为数据库的SID
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance(); //加载数据库驱动
Connection con=DriverManager.getConnection(URL,Username,Password);
PostgreSQL:
String Driver="org.postgresql.Driver"; //连接数据库的方法
String URL="jdbc:postgresql://localhost/db_name"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
DB2:
String Driver="com.ibm.db2.jdbc.app.DB2.Driver"; //连接具有DB2客户端的Provider实例
//String Driver="com.ibm.db2.jdbc.net.DB2.Driver"; //连接不具有DB2客户端的Provider实例
String URL="jdbc:db2://localhost:5000/db_name"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
Informix:
String Driver="com.informix.jdbc.IfxDriver";
String URL="jdbc:Informix-sqli://localhost:1533/db_name:INFORMIXSER=myserver"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
JDBC-ODBC:
String Driver="sun.jdbc.odbc.JdbcOdbcDriver";
String URL="jdbc:odbc:dbsource"; //dbsource为数据源名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
String Driver="com.mysql.jdbc.Driver"; //驱动程序
String URL="jdbc:mysql://localhost:3306/db_name"?useUnicode=true&characterEncoding=UTF-8; //连接的URL,db_name为数据库名,注意修改编码类型
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).new Instance();
Connection con=DriverManager.getConnection(URL,Username,Password);
Microsoft SQL Server 2.0驱动(3个jar的那个):
String Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; //连接SQL数据库的方法
String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name"; //db_name为数据库名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).new Instance(); //加载数据可驱动
Connection con=DriverManager.getConnection(URL,UserName,Password); //
Microsoft SQL Server 3.0驱动(1个jar的那个): // 老紫竹完善
String Driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; //连接SQL数据库的方法
String URL="jdbc:sqlserver://localhost:1433;DatabaseName=db_name"; //db_name为数据库名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).new Instance(); //加载数据可驱动
Connection con=DriverManager.getConnection(URL,UserName,Password); //
Sysbase:
String Driver="com.sybase.jdbc.SybDriver"; //驱动程序
String URL="jdbc:Sysbase://localhost:5007/db_name"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
Oracle(用thin模式):
String Driver="oracle.jdbc.driver.OracleDriver"; //连接数据库的方法
String URL="jdbc:oracle:thin:@loaclhost:1521:orcl"; //orcl为数据库的SID
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance(); //加载数据库驱动
Connection con=DriverManager.getConnection(URL,Username,Password);
PostgreSQL:
String Driver="org.postgresql.Driver"; //连接数据库的方法
String URL="jdbc:postgresql://localhost/db_name"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
DB2:
String Driver="com.ibm.db2.jdbc.app.DB2.Driver"; //连接具有DB2客户端的Provider实例
//String Driver="com.ibm.db2.jdbc.net.DB2.Driver"; //连接不具有DB2客户端的Provider实例
String URL="jdbc:db2://localhost:5000/db_name"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
Informix:
String Driver="com.informix.jdbc.IfxDriver";
String URL="jdbc:Informix-sqli://localhost:1533/db_name:INFORMIXSER=myserver"; //db_name为数据可名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
JDBC-ODBC:
String Driver="sun.jdbc.odbc.JdbcOdbcDriver";
String URL="jdbc:odbc:dbsource"; //dbsource为数据源名
String Username="username"; //用户名
String Password="password"; //密码
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
2013-08-16
展开全部
我之前自学的时候有自己做过一个连接数据库的,你可以参考一下..不同的数据库其实大同小异啦~~
package demo;
import java.sql.*;
import demo.NoticeBean;
public class JDBCTest {
public static void main(String args[]) {
// 连接Oracle 9i及SQL Server 2000的例程
/* try {
String name = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:kevin";
Class.forName(name).newInstance();
Connection con = DriverManager.getConnection(url, "kevin", "kevin");
System.out.println("连接Oracle 9i成功!");
con.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
String name = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=JavaWeb";
Class.forName(name).newInstance();
Connection con = DriverManager.getConnection(url, "sa", "sa");
System.out.println("连接SQL Server 2000成功!");
con.close();
} catch (Exception e) {
e.printStackTrace();
}*/
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
String driverName = "org.gjt.mm.mysql.Driver";
Class.forName(driverName).newInstance();
String url = "jdbc:mysql://localhost:3306/JavaWeb?useUnicode=true&characterEncoding=gb2312";
connection = DriverManager.getConnection(url, "root", "root");
System.out.println("连接MySQL 5.0.18成功!(驱动3.1.12)");
statement = null;
resultSet = null;
String strSql = null;
NoticeBean bean = null;
String title = null;
String content = null;
// 对数据库的插入操作
try {
title = "标题";
content = "内容";
strSql = "INSERT INTO notice(title, content) VALUES('" + title
+ "','" + content + "')";
statement = connection.createStatement();
statement.executeUpdate(strSql);
System.out.println("插入语句执行成功:" + strSql);
} catch (SQLException ex1) {
System.out.println("插入失败!");
}
// 对数据库的查询操作
try {
strSql = "SELECT * FROM notice";
statement = connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
resultSet = statement.executeQuery(strSql);
if (resultSet.next()) {
int id = resultSet.getInt("id");
title = resultSet.getString("title");
content = resultSet.getString("content");
if (resultSet.next()) {
bean = new NoticeBean(id, title, content);
}
System.out.println("notice表的第一行数据是:" + bean.getId() + " "
+ bean.getContent() + " " + bean.getTitle());
}
} catch (SQLException ex1) {
System.out.println("查询失败!");
}
// 对数据库的删除操作
try {
strSql = "DELETE FROM notice";
statement = connection.createStatement();
statement.executeUpdate(strSql);
System.out.println("notice表的所有数据已被删除!");
} catch (SQLException ex1) {
System.out.println("删除失败!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null) {
resultSet.close();
resultSet = null;
}
if (statement != null) {
statement.close();
statement = null;
}
if (connection != null) {
connection.close();
connection = null;
}
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
}
}
当然,之前的配置要做好,你可以去找本书来看,按照书上说的将环境配置好..
package demo;
import java.sql.*;
import demo.NoticeBean;
public class JDBCTest {
public static void main(String args[]) {
// 连接Oracle 9i及SQL Server 2000的例程
/* try {
String name = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:kevin";
Class.forName(name).newInstance();
Connection con = DriverManager.getConnection(url, "kevin", "kevin");
System.out.println("连接Oracle 9i成功!");
con.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
String name = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=JavaWeb";
Class.forName(name).newInstance();
Connection con = DriverManager.getConnection(url, "sa", "sa");
System.out.println("连接SQL Server 2000成功!");
con.close();
} catch (Exception e) {
e.printStackTrace();
}*/
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
String driverName = "org.gjt.mm.mysql.Driver";
Class.forName(driverName).newInstance();
String url = "jdbc:mysql://localhost:3306/JavaWeb?useUnicode=true&characterEncoding=gb2312";
connection = DriverManager.getConnection(url, "root", "root");
System.out.println("连接MySQL 5.0.18成功!(驱动3.1.12)");
statement = null;
resultSet = null;
String strSql = null;
NoticeBean bean = null;
String title = null;
String content = null;
// 对数据库的插入操作
try {
title = "标题";
content = "内容";
strSql = "INSERT INTO notice(title, content) VALUES('" + title
+ "','" + content + "')";
statement = connection.createStatement();
statement.executeUpdate(strSql);
System.out.println("插入语句执行成功:" + strSql);
} catch (SQLException ex1) {
System.out.println("插入失败!");
}
// 对数据库的查询操作
try {
strSql = "SELECT * FROM notice";
statement = connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
resultSet = statement.executeQuery(strSql);
if (resultSet.next()) {
int id = resultSet.getInt("id");
title = resultSet.getString("title");
content = resultSet.getString("content");
if (resultSet.next()) {
bean = new NoticeBean(id, title, content);
}
System.out.println("notice表的第一行数据是:" + bean.getId() + " "
+ bean.getContent() + " " + bean.getTitle());
}
} catch (SQLException ex1) {
System.out.println("查询失败!");
}
// 对数据库的删除操作
try {
strSql = "DELETE FROM notice";
statement = connection.createStatement();
statement.executeUpdate(strSql);
System.out.println("notice表的所有数据已被删除!");
} catch (SQLException ex1) {
System.out.println("删除失败!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null) {
resultSet.close();
resultSet = null;
}
if (statement != null) {
statement.close();
statement = null;
}
if (connection != null) {
connection.close();
connection = null;
}
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
}
}
当然,之前的配置要做好,你可以去找本书来看,按照书上说的将环境配置好..
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-16
展开全部
上百度或者狗哥搜,一搜一大堆,最基础的问题,不要浪费你的积分了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询