Java中如何实现与数据库的连接?在Myeclipse中又如何连接自身数据库????
2个回答
展开全部
链接Oracle 数据库
package com.piqiu.struts.util;
import java.sql.*;
import java.sql.DriverManager;
public class DBConnection {
private String driver="oracle.jdbc.driver.OracleDriver";
private String url="jdbc:oracle:thin:@localhost:1521:PIQIU";
private String user="SCOTT";
private String pwd="fight";
private Connection conn = null;
//得到连接
public Connection getConnection(){
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, pwd);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
// 关闭资源
public void closeAll(PreparedStatement ps,ResultSet rs,Connection conn){
try {
if(rs != null){
rs.close();
}
if(ps != null){
ps.close();
}
if(conn != null){
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
链接sqlserver数据库
package com.svse.struts.util;
import java.sql.*;
public class DBConnection
{
private String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String url = "jdbc:sqlserver://localhost:1433;databaseName=test";
private String username = "sa";
private String password = "sa";
private Connection conn = null;
public Connection getConnection()
{
try
{
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
}
catch (Exception e)
{
e.printStackTrace();
}
return conn;
}
// 关闭
public void closeAll(PreparedStatement pre, ResultSet rs, Connection conn)
{
try
{
if (rs != null)
{
rs.close();
}
if (pre != null)
{
pre.close();
}
if (conn != null)
{
conn.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
package com.piqiu.struts.util;
import java.sql.*;
import java.sql.DriverManager;
public class DBConnection {
private String driver="oracle.jdbc.driver.OracleDriver";
private String url="jdbc:oracle:thin:@localhost:1521:PIQIU";
private String user="SCOTT";
private String pwd="fight";
private Connection conn = null;
//得到连接
public Connection getConnection(){
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, pwd);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
// 关闭资源
public void closeAll(PreparedStatement ps,ResultSet rs,Connection conn){
try {
if(rs != null){
rs.close();
}
if(ps != null){
ps.close();
}
if(conn != null){
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
链接sqlserver数据库
package com.svse.struts.util;
import java.sql.*;
public class DBConnection
{
private String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String url = "jdbc:sqlserver://localhost:1433;databaseName=test";
private String username = "sa";
private String password = "sa";
private Connection conn = null;
public Connection getConnection()
{
try
{
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
}
catch (Exception e)
{
e.printStackTrace();
}
return conn;
}
// 关闭
public void closeAll(PreparedStatement pre, ResultSet rs, Connection conn)
{
try
{
if (rs != null)
{
rs.close();
}
if (pre != null)
{
pre.close();
}
if (conn != null)
{
conn.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询