真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java代碼和數(shù)據(jù)庫(kù)代碼 數(shù)據(jù)庫(kù)和Java

求java與數(shù)據(jù)庫(kù)連接的代碼(含注釋)

代碼主要列出連接數(shù)據(jù)庫(kù)的關(guān)鍵代碼,其他訪問(wèn)數(shù)據(jù)庫(kù)代碼省略

為翠屏等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及翠屏網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、翠屏網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

1、Oracle8/8i/9i數(shù)據(jù)庫(kù)(thin模式)

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

String url="jdbc:oracle:thin:@localhost:1521:orcl";

//orcl為數(shù)據(jù)庫(kù)的SID

String user="test";

String password="test";

Connection conn= DriverManager.getConnection(url,user,password);

2、DB2數(shù)據(jù)庫(kù)

Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();

String url="jdbc:db2://localhost:5000/sample";

//sample為你的數(shù)據(jù)庫(kù)名

String user="admin";

String password="";

Connection conn= DriverManager.getConnection(url,user,password);

3、Sql Server7.0/2000數(shù)據(jù)庫(kù)

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();

String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";

//mydb為數(shù)據(jù)庫(kù)

String user="sa";

String password="";

Connection conn= DriverManager.getConnection(url,user,password);

4、Sybase數(shù)據(jù)庫(kù)

Class.forName("com.sybase.jdbc.SybDriver").newInstance();

String url =" jdbc:sybase:Tds:localhost:5007/myDB";

//myDB為你的數(shù)據(jù)庫(kù)名

Properties sysProps = System.getProperties();

SysProps.put("user","userid");

SysProps.put("password","user_password");

Connection conn= DriverManager.getConnection(url, SysProps);

5、Informix數(shù)據(jù)庫(kù)

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為數(shù)據(jù)庫(kù)名

Connection conn= DriverManager.getConnection(url);

6、MySQL數(shù)據(jù)庫(kù)

Class.forName("org.gjt.mm.mysql.Driver").newInstance();

String url ="jdbc:mysql://localhost/myDB?user=softpassword=soft1234useUnicode=truecharacterEncoding=8859_1"

//myDB為數(shù)據(jù)庫(kù)名

Connection conn= DriverManager.getConnection(url);

7、PostgreSQL數(shù)據(jù)庫(kù)

Class.forName("org.postgresql.Driver").newInstance();

String url ="jdbc:postgresql://localhost/myDB"

//myDB為數(shù)據(jù)庫(kù)名

String user="myuser";

String password="mypassword";

Connection conn= DriverManager.getConnection(url,user,password);

java連接數(shù)據(jù)庫(kù)mysql代碼及簡(jiǎn)單訪問(wèn)數(shù)據(jù)庫(kù)

import java.sql.*;

public class DataBasePractice {

public static void main(String[] args) {

//聲明Connection對(duì)象

Connection con;

//驅(qū)動(dòng)程序名

String driver = "com.mysql.jdbc.Driver";

//URL指向要訪問(wèn)的數(shù)據(jù)庫(kù)名mydata

String url = "jdbc:mysql://localhost:3306/mydata";

//MySQL配置時(shí)的用戶名

String user = "root";

//MySQL配置時(shí)的密碼

String password = "root";

//遍歷查詢結(jié)果集

try {

//加載驅(qū)動(dòng)程序

Class.forName(driver);

//1.getConnection()方法,連接MySQL數(shù)據(jù)庫(kù)!!

con = DriverManager.getConnection(url,user,password);

if(!con.isClosed())

System.out.println("Succeeded connecting to the Database!");

//2.創(chuàng)建statement類對(duì)象,用來(lái)執(zhí)行SQL語(yǔ)句??!

Statement statement = con.createStatement();

//要執(zhí)行的SQL語(yǔ)句

String sql = "select * from student";

//3.ResultSet類,用來(lái)存放獲取的結(jié)果集?。?/p>

ResultSet rs = statement.executeQuery(sql);

System.out.println("-----------------");

System.out.println("執(zhí)行結(jié)果如下所示:");

System.out.println("-----------------");

System.out.println(" 學(xué)號(hào)" + "\t" + " 姓名");

System.out.println("-----------------");

String name = null;

String id = null;

while(rs.next()){

//獲取stuname這列數(shù)據(jù)

name = rs.getString("stuname");

//獲取stuid這列數(shù)據(jù)

id = rs.getString("stuid");

//首先使用ISO-8859-1字符集將name解碼為字節(jié)序列并將結(jié)果存儲(chǔ)新的字節(jié)數(shù)組中。

//然后使用GB2312字符集解碼指定的字節(jié)數(shù)組。

name = new String(name.getBytes("ISO-8859-1"),"gb2312");

//輸出結(jié)果

System.out.println(id + "\t" + name);

}

rs.close();

con.close();

} catch(ClassNotFoundException e) {

//數(shù)據(jù)庫(kù)驅(qū)動(dòng)類異常處理

System.out.println("Sorry,can`t find the Driver!");

e.printStackTrace();

} catch(SQLException e) {

//數(shù)據(jù)庫(kù)連接失敗異常處理

e.printStackTrace();

}catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}finally{

System.out.println("數(shù)據(jù)庫(kù)數(shù)據(jù)成功獲?。?!");

}

}

}

在上面while代碼段后面添加以下代碼段:

String name = null;

String id = null;

while(rs.next()){

//獲取stuname這列數(shù)據(jù)

name = rs.getString("stuname");

//獲取stuid這列數(shù)據(jù)

id = rs.getString("stuid");

//首先使用ISO-8859-1字符集將name解碼為字節(jié)序列并將結(jié)果存儲(chǔ)新的字節(jié)數(shù)組中。

//然后使用GB2312字符集解碼指定的字節(jié)數(shù)組。

name = new String(name.getBytes("ISO-8859-1"),"gb2312");

//輸出結(jié)果

System.out.println(id + "\t" + name);

}

PreparedStatement psql;

ResultSet res;

//預(yù)處理添加數(shù)據(jù),其中有兩個(gè)參數(shù)--“?”

psql = con.prepareStatement("insert into student values(?,?)");

psql.setInt(1, 8); //設(shè)置參數(shù)1,創(chuàng)建id為5的數(shù)據(jù)

psql.setString(2, "xiaogang"); //設(shè)置參數(shù)2,name 為小明

psql.executeUpdate(); //執(zhí)行更新

//預(yù)處理更新(修改)數(shù)據(jù)

psql = con.prepareStatement("update student set stuname = ? where stuid = ?");

psql.setString(1,"xiaowang"); //設(shè)置參數(shù)1,將name改為王五

psql.setInt(2,10); //設(shè)置參數(shù)2,將id為2的數(shù)據(jù)做修改

psql.executeUpdate();

//預(yù)處理刪除數(shù)據(jù)

psql = con.prepareStatement("delete from student where stuid = ?");

psql.setInt(1, 5);

psql.executeUpdate();

//查詢修改數(shù)據(jù)后student表中的數(shù)據(jù)

psql = con.prepareStatement("select*from student");

res = psql.executeQuery(); //執(zhí)行預(yù)處理sql語(yǔ)句

System.out.println("執(zhí)行增加、修改、刪除后的數(shù)據(jù)");

while(res.next()){

name = res.getString("stuname");

id = res.getString("stuid");

name = new String(name.getBytes("ISO-8859-1"),"gb2312");

System.out.println(id + "\t" + name);

}

res.close();

psql.close();

求:用Java連接數(shù)據(jù)庫(kù)和簡(jiǎn)單的數(shù)據(jù)庫(kù)操作代碼

以上的代碼都不如哥的 且看哥是怎么寫條理清晰的代碼的!?。?/p>

package dbconnection //java 中不存在沒(méi)有包的類(講解詳細(xì)因?yàn)轫?xiàng)目需要)

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;//引入sql數(shù)據(jù)庫(kù)包

public class DBConnection{

private Connection conn=null;

private Statement stmt=null;

private Result rs=null;

private String jdbc="com.microsoft.sqlserver.jdbc.SQLServerDriver";

private String driverManager="jdbc:sqlserver://localhost:1433;databasename=HcitPos";

private String user="admin";

private int password="admin";

public DBConnection{

try{

Class.forName("jdbc");

conn.getConnection("driverManager");

}

catch(Exception e){}

}

public selectMethod(String sql){

stmt=conn.createStatement();

rs=stmt.extcuteQuery("sql");

while(rs.next()){

String title=rs.getString("title");//利用javaBean獲得數(shù)據(jù)庫(kù)中的屬性

String name=rs.getString("name");

.......

System.out.println("title");

System.out.println("name");

......

//當(dāng)然數(shù)據(jù)庫(kù)的操作有很多 這里簡(jiǎn)單介紹下功能的實(shí)現(xiàn)

}

}

public void closeDB(){

if(rs != null) rs.close();

if(stmt != null) stmt.close();

if(conn != null) conn.close();

}

}

java連接數(shù)據(jù)庫(kù)的代碼

用這個(gè)類吧.好的話,給我加加分.

import java.sql.*;

/**

* @功能: 一個(gè)JDBC的本地化API連接類,封裝了數(shù)據(jù)操作方法,只用傳一個(gè)SQL語(yǔ)句即可

* @作者: 李開歡

* @日期: 2007/

*/

public class ConnectionDemo {

/*

* 這里可以將常量全部放入另一個(gè)類中,以方便修改

*/

private static Connection conn;

private static Statement ps;

private static ResultSet rs;

private static final String DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

private static final String URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";

private static final String USER ="sa";

private static final String PASS = "sa";

public ConnectionDemo() {

// TODO Auto-generated constructor stub

ConnectionDemo.getConnection();

}

public static Connection getConnection(){

System.out.println("連接中...");

try {

Class.forName(ConnectionDemo.DRIVER);

conn = DriverManager.getConnection(ConnectionDemo.URL, ConnectionDemo.USER, ConnectionDemo.PASS);

System.out.println("成功連接");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return conn;

}

public static Statement getStatement(String sql){

System.out.println("執(zhí)行SQL語(yǔ)句中...");

try {

ps = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

if(sql.substring(0, 6).equals("select")){

rs = ps.executeQuery(sql);

System.out.println("執(zhí)行完查詢操作,結(jié)果已返回ResultSet集合");

}else if(sql.substring(0, 6).equals("delete")){

ps.executeUpdate(sql);

System.out.println("已執(zhí)行完畢刪除操作");

}else if(sql.substring(0, 6).equals("insert")){

ps.executeUpdate(sql);

System.out.println("已執(zhí)行完畢增加操作");

}else{

ps.executeUpdate(sql);

System.out.println("已執(zhí)行完畢更新操作");

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return ps;

}

public static ResultSet getResultSet(){

System.out.println("查詢結(jié)果為:");

return rs;

}

public static void closeConnection(){

System.out.println("關(guān)閉連接中...");

try {

if (rs != null) {

rs.close();

System.out.println("已關(guān)閉ResultSet");

}

if (ps != null) {

ps.close();

System.out.println("已關(guān)閉Statement");

}

if (conn != null) {

conn.close();

System.out.println("已關(guān)閉Connection");

}

} catch (Exception e) {

// TODO: handle exception

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

ConnectionDemo.getConnection();

String sql = "delete from type where id = 1";

ConnectionDemo.getStatement(sql);

String sql2 = "insert into type values(1,'教學(xué)設(shè)備')";

ConnectionDemo.getStatement(sql2);

String sql1 = "select * from type";

ConnectionDemo.getStatement(sql1);

ResultSet rs = ConnectionDemo.getResultSet();

System.out.println("編號(hào) "+"類 型");

try {

while(rs.next()){

System.out.print(" "+rs.getInt(1)+" ");

System.out.println(rs.getString(2));

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

ConnectionDemo.closeConnection();

}

}

是把java代碼和數(shù)據(jù)庫(kù)代碼 分別編好后在連接 還是一起編程a啊

分開,在數(shù)據(jù)庫(kù)中的將你的代碼用一個(gè)數(shù)據(jù)庫(kù)腳本存放好,這樣可以確保你使用的sql語(yǔ)句是對(duì)的,在JAVA代碼中需要哪條數(shù)據(jù)就將他Copy過(guò)來(lái)就是。

JAVA連接數(shù)據(jù)庫(kù)連接代碼怎么寫?

1 將數(shù)據(jù)庫(kù)的JDBC驅(qū)動(dòng)加載到classpath中,在基于JAVAEE的WEB應(yīng)用實(shí)際開發(fā)過(guò)程中,通常要把目標(biāo)數(shù)據(jù)庫(kù)產(chǎn)品的JDBC驅(qū)動(dòng)復(fù)制到WEB-INF/lib下.

2 加載JDBC驅(qū)動(dòng),并將其注冊(cè)到DriverManager中,下面是一些主流數(shù)據(jù)庫(kù)的JDBC驅(qū)動(dòng)加裁注冊(cè)的代碼:

//Oracle8/8i/9iO數(shù)據(jù)庫(kù)(thin模式)

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

//Sql Server7.0/2000數(shù)據(jù)庫(kù)

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();

//DB2數(shù)據(jù)庫(kù)

Class.froName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();

//Informix數(shù)據(jù)庫(kù)

Class.forName("com.informix.jdbc.IfxDriver").newInstance();

//Sybase數(shù)據(jù)庫(kù)

Class.forName("com.sybase.jdbc.SybDriver").newInstance();

//MySQL數(shù)據(jù)庫(kù)

Class.forName("com.mysql.jdbc.Driver").newInstance();

//PostgreSQL數(shù)據(jù)庫(kù)

Class.forNaem("org.postgresql.Driver").newInstance();

3 建立數(shù)據(jù)庫(kù)連接,取得Connection對(duì)象.例如:

//Oracle8/8i/9i數(shù)據(jù)庫(kù)(thin模式)

String url="jdbc:oracle:thin:@localhost:1521:orcl";

String user="scott";

String password="tiger";

Connection conn=DriverManager.getConnection(url,user,password);

--完整的太多了!我已經(jīng)把完整的代碼發(fā)到你QQ郵箱了!


標(biāo)題名稱:java代碼和數(shù)據(jù)庫(kù)代碼 數(shù)據(jù)庫(kù)和Java
分享鏈接:http://weahome.cn/article/hihpjo.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部