❶ eclipse連接sql server2008r2jar包導進去了還是顯示驅動有問題怎麼辦
eclipse
連接sql server2008r2
時遇到問題,即使導入了jar:sqljdbc4-6.0.6629.101.jar,仍然提示「載入錯誤------Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")」。解決方法如下:
首先,下載SQL Server驅動包,內含sqljdbc.jar。
在建立的工程下操作:
project->properties->java Build Path->add External Jars
將sqljdbc.jar添加至工程中。
將jar包導入到java工程中,確保其安全性與兼容性。
注意,tomcat作為web伺服器,如果你的程序包含jsp頁面並需要發布,那麼需要將jar包配置到tomcat的lib目錄下。
如果你的程序僅為簡單的java連接資料庫代碼,無需依賴tomcat,直接將兩個jar包導入到java工程即可。
❷ JAVA web 與資料庫的連接到底是怎樣連的啊
JAVA Web開發中與資料庫的連接操作,配置:
1、新建資料庫。
新建登錄角色,在新建資料庫的時候把資料庫的所有權交給你新建的角色。用用戶和密碼控制資料庫。保證資料庫的安全。
2、編寫context.xml文件 Xml文件的目的是封裝用戶和密碼,也是封裝的一種,方便操作。
以下為context.xml文件樣例:
<?xml version="1.0" encoding="utf-8"?>
<Context reloadable = "true">
<Resource
name="jdbc/sampleHS"
type="javax.sql.DataSource"
maxActive="14"
maxIdle="10"
username="hstaoshu"
maxWait="5000"
driverClassName="org.postgresql.Driver"
password="hstaoshu"
url="jdbc:postgresql://localhost:5432/hstaoshu"/>
</Context>
詳細說明:
name="jdbc/sampleHS"裡面的ssampHS是可改名稱,建議根據需要自己命名;
username="hstaoshu"
password="hstaoshu"此兩項為你新建的資料庫登錄角色用戶名和密碼信息,只有匹配 了才能訪問。這里簡單為了表示,把用戶名和密碼弄成了跟資料庫名字一樣。其實這是很不安全的。
url="jdbc:postgresql://localhost:5432/hstaoshu"/>
這是連接資料庫的URl,就像訪問網站的地址一樣。沒有這個是無法訪問資料庫的。localhost:5432表示本地埠。一般不需要改動,如果你在配置資料庫的時候改動過埠,那麼你需要把它改回來。/hstaoshu是你的資料庫名稱。
其他選項請勿擅自改動。
3、編寫DAO類。
DAO類的作用是與數據連接後,對資料庫的一些操作的封裝。封裝的作用。為了更好的數據管理。
DAO是真正如何使用資料庫的關鍵步驟,前兩步只是部署和配置。
private static InitialContext context = null;
private DataSource dataSource = null;
//一般把跟資料庫的連接放在DAO類的構造函數里,只要被實例化,就能和資料庫連接。
public BookDAO() {
try {
if (context == null) {
context = new InitialContext();
}
dataSource = (DataSource) context.lookup("java:comp/env/jdbc/sampleHS");
// 連接資料庫,前面在context.xml文件配置里的URl
} catch (NamingException e2) {
e2.printStackTrace();
}
}
public Connection getConnection() {
Connection conn = null;
try {
conn = dataSource.getConnection();// 獲得數據源的連接對象
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
做完上面的三步操作,開發環境已經完全和資料庫連接OK,可以開始資料庫的操作了。一般來說,對資料庫的操作語句都是提前封裝好的。這樣修改起來會對下面的代碼影響降到最小。
如下:
// ------------------資料庫操作語句代碼封裝------------------
/* 查看所有圖書 */
private static final String SELECT_ALL_SQL = "SELECT * FROM book";
那麼在使用的時候只要直接調用:
pstmt = conn.prepareStatement(SELECT_ALL_SQL);
❸ Java Web與資料庫連接
jsp連接Oracle8/8i/9i資料庫(用thin模式)
testoracle.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl為你的資料庫的SID
String user="scott";
String password="tiger";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一個欄位內容為:<%=rs.getString(1)%>
您的第二個欄位內容為:<%=rs.getString(2)%>
<%}%>
<%out.print("資料庫操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
二、jsp連接Sql Server7.0/2000資料庫
testsqlserver.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
//pubs為你的資料庫的
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一個欄位內容為:<%=rs.getString(1)%>
您的第二個欄位內容為:<%=rs.getString(2)%>
<%}%>
<%out.print("資料庫操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
三、jsp連接DB2資料庫
testdb2.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%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);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一個欄位內容為:<%=rs.getString(1)%>
您的第二個欄位內容為:<%=rs.getString(2)%>
<%}%>
<%out.print("資料庫操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
四、jsp連接Informix資料庫
testinformix.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.informix.jdbc.IfxDriver").newInstance();
String url =
"jdbc:informix-sqli://123.45.67.89:1533/testDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword";
//testDB為你的資料庫名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一個欄位內容為:<%=rs.getString(1)%>
您的第二個欄位內容為:<%=rs.getString(2)%>
<%}%>
<%out.print("資料庫操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
五、jsp連接Sybase資料庫
testmysql.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/tsdata";
//tsdata為你的資料庫名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一個欄位內容為:<%=rs.getString(1)%>
您的第二個欄位內容為:<%=rs.getString(2)%>
<%}%>
<%out.print("資料庫操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
六、jsp連接MySQL資料庫
testmysql.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//testDB為你的資料庫名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一個欄位內容為:<%=rs.getString(1)%>
您的第二個欄位內容為:<%=rs.getString(2)%>
<%}%>
<%out.print("資料庫操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
七、jsp連接PostgreSQL資料庫
testmysql.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/soft"
//soft為你的資料庫名
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一個欄位內容為:<%=rs.getString(1)%>
您的第二個欄位內容為:<%=rs.getString(2)%>
<%}%>
<%out.print("資料庫操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
其中的核心鏈接代碼,你自己比對一下看看寫的對不對,有的資料庫連接是需要jar驅動包的
1、Oracle8/8i/9i資料庫(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl為資料庫的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
2、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);
3、Sql Server7.0/2000資料庫
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb為資料庫
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
4、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);
5、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);
6、MySQL資料庫
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/myDB?user=softamp;password=soft1234amp;useUnicode=trueamp;characterEncoding=8859_1"
//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);