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);
Ⅱ 求助.webservice用JAVA怎麼連接資料庫 進行開發
假設有個web服務查詢資料庫並返回一個dataset,在客戶端只要添加引用,然後調用web服務方法即可;
1.工程-添加web引用-瀏覽-http://localhost/mydir/datasetservice.asmx
2.localhost.datasetservice
service;
dataset
ds;
service=new
localhost.datasetservice();
ds=service.gettitles();
mydatagrid.datasource=ds;
Ⅲ myeclipse開發javaweb怎樣連接資料庫
如果直接用java代碼連接資料庫的話,1.導入資料庫驅動包,2.載入資料庫驅動,3.Connection連接資料庫就好;
如果要資料庫連接池技術就要配置了
Ⅳ java web連接資料庫
首先你要檢查你的資料庫的
埠號
是否正確,接下來你要確定項目中是否導入數據連接的JAR包,再接著就是看你連接的資料庫
SQLserver
是否開啟,數據表是否存在於資料庫中,最後看你的資料庫賬號,密碼是否設置正確
Ⅳ JAVA WEB(MyEclipse 10)連接資料庫具體怎麼操作
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Connection;
importjava.sql.Statement;
publicclassMysqlDemo{
publicstaticvoidmain(String[]args)throwsException{
Connectionconn=null;
Stringsql;
//MySQL的JDBCURL編寫方式:jdbc:mysql://主機名稱:連接埠/資料庫的名稱?參數=值
//避免中文亂碼要指定useUnicode和characterEncoding
//執行資料庫操作之前要在資料庫管理系統上創建一個資料庫,名字自己定,
//下面語句之前就要先創建javademo資料庫
Stringurl="jdbc:mysql://localhost:3306/javademo?"
+"user=root&password=root&useUnicode=true&characterEncoding=UTF8";
try{
//之所以要使用下面這條語句,是因為要使用MySQL的驅動,所以我們要把它驅動起來,
//可以通過Class.forName把它載入進去,也可以通過初始化來驅動起來,下面三種形式都可以
Class.forName("com.mysql.jdbc.Driver");//動態載入mysql驅動
//or:
//com.mysql.jdbc.Driverdriver=newcom.mysql.jdbc.Driver();
//or:
//newcom.mysql.jdbc.Driver();
System.out.println("成功載入MySQL驅動程序");
//一個Connection代表一個資料庫連接
conn=DriverManager.getConnection(url);
//Statement裡面帶有很多方法,比如executeUpdate可以實現插入,更新和刪除等
Statementstmt=conn.createStatement();
sql="createtablestudent(NOchar(20),namevarchar(20),primarykey(NO))";
intresult=stmt.executeUpdate(sql);//executeUpdate語句會返回一個受影響的行數,如果返回-1就沒有成功
if(result!=-1){
System.out.println("創建數據表成功");
sql="insertintostudent(NO,name)values('2012001','陶偉基')";
result=stmt.executeUpdate(sql);
sql="insertintostudent(NO,name)values('2012002','周小俊')";
result=stmt.executeUpdate(sql);
sql="select*fromstudent";
ResultSetrs=stmt.executeQuery(sql);//executeQuery會返回結果的集合,否則返回空值
System.out.println("學號 姓名");
while(rs.next()){
System.out
.println(rs.getString(1)+" "+rs.getString(2));//入如果返回的是int類型可以用getInt()
}
}
}catch(SQLExceptione){
System.out.println("MySQL操作錯誤");
e.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
}finally{
conn.close();
}
}
}
Ⅵ javaweb怎麼連接mysql資料庫
當然,首先要安裝有JDK(一般是JDK1.5.X)。然後安裝MySQL,這些都比較簡單,具體過程就不說了。配置好這兩個環境後,下載JDBC
驅動mysql-connector-java-5.0.5.zip(這個是最新版的)。然後將其解壓縮到任一目錄。我是解壓到D盤,然後將其目錄下的
mysql-connector-java-5.0.5-bin.jar加到classpath里,
具體如下:「我的電腦」-> 「屬性」 -> 「高級」 ->
「環境變數」,在系統變數那裡編輯classpath,將D:\mysql-connector-java-5.0.5\mysql-
connector-java-5.0.5-bin.jar加到最後,在加這個字元串前要加「;」,以與前一個classpath區分開。然後確定。
環境配置好了,很簡單。現在,先配置Java連接MySQL,設其用戶名為「root」,密碼為「root」。在命令行或用一個SQL的前端軟體創建Database。
我是用SQLyog的前端軟體來創建Database的。
先創建資料庫:
CREATE DATABASE SCUTCS;
接著,創建表:
CREATE TABLE STUDENT ( SNO CHAR(7) NOT NULL, SNAME VARCHAR(8) NOT NULL, SEX CHAR(2) NOT NULL, BDATE DATE NOT NULL, HEIGHT DEC(5,2) DEFAULT 000.00, PRIMARY KEY(SNO) );
然後插入數據,可以用SQL語句insert into <表名> values (value1, value2, ...);
也可以用SQLyog來操作
好了,創建好了。
下面,我們來編寫.java文件來演示一下如何訪問Java連接MySQL資料庫。
import java.sql.*; public class JDBCTest { public static void main(String[] args){
驅動程序名
String driver = "com.mysql.jdbc.Driver";
// URL指向要訪問的資料庫名scutcs
String url = "jdbc:mysql://127.0.0.1:3306/scutcs";
// MySQL配置時的用戶名
String user = "root";
// Java連接MySQL配置時的密碼
String password = "root";
try {
// 載入驅動程序
Class.forName(driver);
// 連續資料庫
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
// statement用來執行SQL語句
Statement statement = conn.createStatement();
// 要執行的SQL語句
String sql = "select * from student";
結果集
ResultSet rs = statement.executeQuery(sql); System.out.println("-----------------"); System.out.println("執行結果如下所示:"); System.out.println("-----------------"); System.out.println(" 學號" + "\t" + " 姓名"); System.out.println("-----------------"); String name = null; while(rs.next()) {
選擇sname這列數據
name = rs.getString("sname");
// 首先使用ISO-8859-1字元集將name解碼為位元組序列並將結果存儲新的位元組數組中。
// 然後使用GB2312字元集解碼指定的位元組數組
name = new String(name.getBytes("ISO-8859-1"),"GB2312");
// 輸出結果
System.out.println(rs.getString("sno") + "\t" + name); } rs.close(); conn.close(); } catch(ClassNotFoundException e) { System.out.println("Sorry,can`t find the Driver!"); e.printStackTrace(); } catch(SQLException e) { e.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } } }
接下來我們運行一下看下效果:
D:\testjdbc>javac JDBCTest.java
D:\testjdbc>java JDBCTest
Succeeded connecting to the Database!
Ⅶ java web項目中的資料庫連接
首先你要檢查你的資料庫的埠號是否正確,接下來你要確定項目中是否導入數據連接的jar包,再接著就是看你連接的資料庫sqlserver是否開啟,數據表是否存在於資料庫中,最後看你的資料庫賬號,密碼是否設置正確
Ⅷ javaweb怎麼連接訪問資料庫實現登錄
前端頁面傳過來用戶名和密碼,然後java後端通過jdbc去連接資料庫,查找相應的表,比較,對的就登陸成功。
Ⅸ 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);
Ⅹ java web與資料庫相連,具體怎麼做
1.首先要移動mysql-connector-java-5.1.44-bin.jar到tomactde的lib目錄下(我的目錄是這樣:F: omcatapache-tomcat-7.0.63lib)這是一個連接資料庫要用到包,一般在下載mysql的時候選擇配置會下載,然後移動到tomact的lib下;
拓展資料:
Java Web,是用Java技術來解決相關web互聯網領域的技術總和。web包括:web伺服器和web客戶端兩部分。Java在客戶端的應用有java applet,不過使用得很少,Java在伺服器端的應用非常的豐富,比如Servlet,JSP和第三方框架等等。Java技術對Web領域的發展注入了強大的動力。
Java的Web框架雖然各不相同,但基本也都是遵循特定的路數的:使用Servlet或者Filter攔截請求,使用MVC的思想設計架構,使用約定,XML或 Annotation實現配置,運用Java面向對象的特點,面向對象實現請求和響應的流程,支持Jsp,Freemarker,Velocity等視圖。