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

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

java怎么和數(shù)據(jù)庫連接

使用java連接MySQL數(shù)據(jù)庫與其他的數(shù)據(jù)庫連接核心是一樣的,如果說區(qū)別,那就是所需的驅(qū)動不一樣。

公司主營業(yè)務(wù):成都網(wǎng)站建設(shè)、做網(wǎng)站、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出青銅峽免費(fèi)做網(wǎng)站回饋大家。

工具/原料

MySQL、JDK

方法/步驟

1、首先需要安裝好JDK(配置環(huán)境變量),如圖所示:

2、其次要安裝好MySQL數(shù)據(jù)庫,可以使用可視化Navicar For MySQL,如圖所示:

3、最后通過代碼進(jìn)行連接。

(1)確定連接路徑URL:

String url="jdbc:mysql://localhost(可以是本機(jī)IP地址):3306(端口號)/mysqltest(數(shù)據(jù)庫名稱)?"+"user=用戶賬號password=用戶密碼useUnicode=字符編碼";

(2)加載驅(qū)動:

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

(3)連接,獲取Connection對象

Connection conn=DriverManager.getConnection(url)

(4)可以通過conn對象檢驗(yàn)連接與否。

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郵箱了!

連接數(shù)據(jù)庫的代碼用java怎么做?

import java.sql.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

public class inensshow extends JFrame {

private Connection connection;

private Statement statement;

private ResultSet resultSet;

private ResultSetMetaData rsMetaData;

//GUI變量定義

private JTable table;

private JTextArea inputQuery;

private JButton submitQuery;

public inensshow()

{

//Form的標(biāo)題

super( "輸入SQL語句,按查詢按鈕查看結(jié)果。" );

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

String username = "inens";

String password = "inens";

//加載驅(qū)動程序以連接數(shù)據(jù)庫

try {

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

connection = DriverManager.getConnection(

url, username, password );

}

//捕獲加載驅(qū)動程序異常

catch ( ClassNotFoundException cnfex ) {

System.err.println(

"裝載 JDBC/ODBC 驅(qū)動程序失敗。" );

cnfex.printStackTrace();

System.exit( 1 ); // terminate program

}

//捕獲連接數(shù)據(jù)庫異常

catch ( SQLException sqlex ) {

System.err.println( "無法連接數(shù)據(jù)庫" );

sqlex.printStackTrace();

System.exit( 1 ); // terminate program

}

//如果數(shù)據(jù)庫連接成功,則建立GUI

//SQL語句

String test="SELECT * FROM data";

inputQuery = new JTextArea( test, 4, 30 );

submitQuery = new JButton( "查詢" );

//Button事件

submitQuery.addActionListener(

new ActionListener() {

public void actionPerformed( ActionEvent e )

{

getTable();

}

}

);

JPanel topPanel = new JPanel();

topPanel.setLayout( new BorderLayout() );

//將"輸入查詢"編輯框布置到 "CENTER"

topPanel.add( new JScrollPane( inputQuery), BorderLayout.CENTER );

//將"提交查詢"按鈕布置到 "SOUTH"

topPanel.add( submitQuery, BorderLayout.SOUTH );

table = new JTable();

Container c = getContentPane();

c.setLayout( new BorderLayout() );

//將"topPanel"編輯框布置到 "NORTH"

c.add( topPanel, BorderLayout.NORTH );

//將"table"編輯框布置到 "CENTER"

c.add( table, BorderLayout.CENTER );

getTable();

setSize( 500, 300 );

//顯示Form

show();

}

private void getTable()

{

try {

//執(zhí)行SQL語句

String query = inputQuery.getText();

statement = connection.createStatement();

resultSet = statement.executeQuery( query );

//在表格中顯示查詢結(jié)果

displayResultSet( resultSet );

}

catch ( SQLException sqlex ) {

sqlex.printStackTrace();

}

}

private void displayResultSet( ResultSet rs )

throws SQLException

{

//定位到達(dá)第一條記錄

boolean moreRecords = rs.next();

//如果沒有記錄,則提示一條消息

if ( ! moreRecords ) {

JOptionPane.showMessageDialog( this,

"結(jié)果集中無記錄" );

setTitle( "無記錄顯示" );

return;

}

Vector columnHeads = new Vector();

Vector rows = new Vector();

try {

//獲取字段的名稱

ResultSetMetaData rsmd = rs.getMetaData();

for ( int i = 1; i = rsmd.getColumnCount(); ++i )

columnHeads.addElement( rsmd.getColumnName( i ) );

//獲取記錄集

do {

rows.addElement( getNextRow( rs, rsmd ) );

} while ( rs.next() );

//在表格中顯示查詢結(jié)果

table = new JTable( rows, columnHeads );

JScrollPane scroller = new JScrollPane( table );

Container c = getContentPane();

c.remove(1);

c.add( scroller, BorderLayout.CENTER );

//刷新Table

c.validate();

}

catch ( SQLException sqlex ) {

sqlex.printStackTrace();

}

}

private Vector getNextRow( ResultSet rs,

ResultSetMetaData rsmd )

throws SQLException

{

Vector currentRow = new Vector();

for ( int i = 1; i = rsmd.getColumnCount(); ++i )

currentRow.addElement( rs.getString( i ) );

//返回一條記錄

return currentRow;

}

public void shutDown()

{

try {

//斷開數(shù)據(jù)庫連接

connection.close();

}

catch ( SQLException sqlex ) {

System.err.println( "Unable to disconnect" );

sqlex.printStackTrace();

}

}

public static void main( String args[] )

{

final inensshow app =

new inensshow();

app.addWindowListener(

new WindowAdapter() {

public void windowClosing( WindowEvent e )

{

app.shutDown();

System.exit( 0 );

}

}

);

}

}

------------------------------------------------------------

這次在WIN98中就不好使了。因?yàn)镸ysql的驅(qū)動程序沒有也沒能加入到CLASSPATH 當(dāng)中,但是JSP卻可以使用(JSP的98驅(qū)動加載詳見Jsp與Mysql連接查錯文章),所以這次我是在XPServer中測試的。

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

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

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ù)庫的代碼 請教

private static String url="jdbc:oracle:thin:@localhost:1521:xe";

聲明一個字符串用于存儲數(shù)據(jù)庫連接信息,jdbc:oracle:thin:@localhost:1521表示你要連接的是oracle數(shù)據(jù)庫地址是本機(jī)

xe為本機(jī)數(shù)據(jù)庫庫名。

private static String driverName="oracle.jdbc.driver.OracleDriver";

這一條是聲明一個字符串存儲數(shù)據(jù)庫驅(qū)動

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();

}

}

分享標(biāo)題:java數(shù)據(jù)庫的連接代碼,Java連接數(shù)據(jù)庫代碼
文章起源:http://chinadenli.net/article34/heshse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、App開發(fā)全網(wǎng)營銷推廣、外貿(mào)網(wǎng)站建設(shè)、服務(wù)器托管、面包屑導(dǎo)航

廣告

聲明:本網(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)

成都網(wǎng)站建設(shè)公司