日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
java連接jdbc代碼,java連接數(shù)據(jù)庫jdbc

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

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

雞西梨樹網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,雞西梨樹網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為雞西梨樹近千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設(shè)要多少錢,請找那個(gè)售后服務(wù)好的雞西梨樹做網(wǎng)站的公司定做!

import java.sql.*;

/**

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

* @作者: 李開歡

* @日期: 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語句中...");

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代碼,編寫JDBC連接數(shù)據(jù)庫新增員工信息的步驟. 員工信息表:t_emp(id int?

第一步:新建數(shù)據(jù)庫

連接的是本地localhost,新建一個(gè)新的數(shù)據(jù)庫名是jdbctest

然后建表t_emp

不會的話可通過執(zhí)行下方的sql語句建表

CREATE TABLE `t_emp` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`name` varchar(255) DEFAULT NULL,

`salary` double DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

第二步:新建java項(xiàng)目

新建完以后添加mysql驅(qū)動的jar包,jar包自己下載

在項(xiàng)目上右鍵鼠標(biāo)屬性,然后

添加jar包,我這里已經(jīng)加載過了

第三步:編寫代碼

package com.gf;

import java.sql.DriverManager;

import java.sql.SQLException;

import com.mysql.jdbc.Connection;

import com.mysql.jdbc.Statement;

public class Test {

public static void main(String[] args) throws Exception {

int flag=0;

//1.加載驅(qū)動

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

//2.獲取連接

Connection conn=(Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbctest?user=rootpassword=123456useUnicode=truecharacterEncoding=UTF-8");

//3.創(chuàng)建statement

Statement sm=(Statement) conn.createStatement();

//4.執(zhí)行sql語句

flag=sm.executeUpdate("insert into t_emp(name,salary) values('菲菲',34.9)");

if(flag!=0) {

System.out.println("員工信息增加成功");

}else {

System.out.println("添加失敗");

}

}

}

注意點(diǎn):

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

DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbctest?user=rootpassword=123456useUnicode=truecharacterEncoding=UTF-8");

這里需要修改自己本機(jī)的連接信息,不然會出現(xiàn)連接失敗

最后的執(zhí)行結(jié)果

java 實(shí)現(xiàn) jdbc連接oracle 數(shù)據(jù)庫的java代碼怎么寫

package com.jdbc.test;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

public class JDBCTest {

public static void main(String[] args) throws Exception {

//1.加載驅(qū)動

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

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

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

//2.創(chuàng)建數(shù)據(jù)庫連接對象

//Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=db","sa","sqlpass");

//Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db?useUnicode=truecharacterEncoding=UTF-8","root","123456");

Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","Oracle123");

//3.創(chuàng)建數(shù)據(jù)庫命令執(zhí)行對象

Statement stmt = conn.createStatement();

// PreparedStatement ps = conn.prepareStatement("select * from t_user");

//4.執(zhí)行數(shù)據(jù)庫命令

ResultSet rs = stmt.executeQuery("select * from t_user");

// ResultSet rs = ps.executeQuery();

//5.處理執(zhí)行結(jié)果

while (rs.next()) {

int id = rs.getInt("id");

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

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

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

}

//6.釋放數(shù)據(jù)庫資源

if (rs != null) {

rs.close();

}

// if (ps != null) {

// ps.close();

// }

if (stmt != null) {

stmt.close();

}

if (conn != null) {

conn.close();

}

}

}

這是以前寫的3中數(shù)據(jù)庫的鏈接已經(jīng)測試過了。


網(wǎng)頁名稱:java連接jdbc代碼,java連接數(shù)據(jù)庫jdbc
標(biāo)題鏈接:http://www.dlmjj.cn/article/phchsi.html