欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

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

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

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

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

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

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

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

String user="test";

String password="test";

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

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

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

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

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

String user="admin";

String password="";

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

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

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

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

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

String user="sa";

String password="";

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

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

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

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

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

Properties sysProps = System.getProperties();

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

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

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

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

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ù)庫名

Connection conn= DriverManager.getConnection(url);

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

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

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

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

Connection conn= DriverManager.getConnection(url);

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

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

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

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

String user="myuser";

String password="mypassword";

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

java連接數(shù)據(jù)庫mysql代碼及簡單訪問數(shù)據(jù)庫

import java.sql.*;

public class DataBasePractice {

public static void main(String[] args) {

//聲明Connection對象

Connection con;

//驅(qū)動程序名

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

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

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

//MySQL配置時的用戶名

String user = "root";

//MySQL配置時的密碼

String password = "root";

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

try {

//加載驅(qū)動程序

Class.forName(driver);

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

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

if(!con.isClosed())

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

//2.創(chuàng)建statement類對象,用來執(zhí)行SQL語句!!

Statement statement = con.createStatement();

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

String sql = "select * from student";

//3.ResultSet類,用來存放獲取的結(jié)果集!!

ResultSet rs = statement.executeQuery(sql);

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

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

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

System.out.println(" 學(xué)號" + "\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é)果存儲新的字節(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ù)庫驅(qū)動類異常處理

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

e.printStackTrace();

} catch(SQLException e) {

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

e.printStackTrace();

}catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}finally{

System.out.println("數(shù)據(jù)庫數(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é)果存儲新的字節(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ù),其中有兩個參數(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語句

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ù)庫和簡單的數(shù)據(jù)庫操作代碼

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

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

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

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

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ù)庫中的屬性

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

.......

System.out.println("title");

System.out.println("name");

......

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

}

}

public void closeDB(){

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

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

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

}

}

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

用這個類吧.好的話,給我加加分.

import java.sql.*;

/**

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

* @作者: 李開歡

* @日期: 2007/

*/

public class ConnectionDemo {

/*

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

*/

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語句中...");

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("編號 "+"類 型");

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ù)庫代碼 分別編好后在連接 還是一起編程a啊

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

//Oracle8/8i/9i數(shù)據(jù)庫(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郵箱了!

當(dāng)前題目:java代碼和數(shù)據(jù)庫代碼 數(shù)據(jù)庫和Java
網(wǎng)頁路徑:http://chinadenli.net/article20/hihpjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)面包屑導(dǎo)航網(wǎng)站排名App設(shè)計(jì)網(wǎng)站建設(shè)服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)