當前位置:首頁 » 編程語言 » java資料庫實現

java資料庫實現

發布時間: 2022-05-13 17:25:00

㈠ 如何用java實現mysql資料庫的導入導出

MySql導出資料庫的命令如下:
Sql代碼
mysqlmp -uusername -ppassword -hhost -Pport exportDatabaseName > exportPath
mysqlmp -uusername -ppassword -hhost -Pport exportDatabaseName > exportPath

利用Java調用命令窗口執行命令來進行MySql導入資料庫一般分三步走:
第一步:登錄Mysql資料庫,在登錄資料庫的時候也可以指定登錄到哪個資料庫,如果指定了則可以跳過第二步;
第二步:切換資料庫到需要導入的目標資料庫
第三步:利用命令開始導入

在進行導出的時候,需要注意命令語句的運行環境,如果已經將mysql安裝路徑下的bin加入到
系統的path變數中,那麼在導出的時候可以直接使用命令語句,否則,就需要在執行命令語句的
時候加上命令所在位置的路徑,即mysql安裝路徑想的bin下的mysqlmp命令。

㈡ 用JAVA實現資料庫查詢

import java.sql.*;
public class MSSQLText
{
public static void main(String args[])
{
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind";
String user="sa";//這里替換成你自已的資料庫用戶名
String password="sa";//這里替換成你自已的資料庫用戶密碼
String sqlStr="select CustomerID, CompanyName, ContactName from Customers";
try
{ //這里的異常處理語句是必需的.否則不能通過編譯!
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("類實例化成功!");

Connection con = DriverManager.getConnection(url,user,password);
System.out.println("創建連接對像成功!");

Statement st = con.createStatement();
System.out.println("創建Statement成功!");

ResultSet rs = st.executeQuery(sqlStr);
System.out.println("操作數據表成功!");
System.out.println("----------------!");

while(rs.next())
{
System.out.print(rs.getString("CustomerID") + " ");
System.out.print(rs.getString("CompanyName") + " ");
System.out.println(rs.getString("ContactName"));
}
rs.close();
st.close();
con.close();
}
catch(Exception err){
err.printStackTrace(System.out);
}
}
}

㈢ 怎樣才能實現用Java對資料庫中數據的操作

這個就看你的資料庫設計了,比如說兩個資料庫,一個是存種類(酒,菜。。),一個是存種類裡面的分類(白酒,啤酒。。),然後在項目中鏈接資料庫進行操作

㈣ JAVA如何實現資料庫的批處理操作

批量數據進入資料庫使用addBatch()和executeBatch()方法
PreparedStatement.addBatch(); ...... PreparedStatement.executeBatch();需要注意的是一次最多不要超過50條:1.因為插入的時候資料庫已經鎖定,然而若是一次性插入太多會造成其他業務的等待。2.會造成內存的溢出
舉例:

PreparedStatement pst = (PreparedStatement) con.prepareStatement("insert into ***** values (?,'***')"); for (int i = 0; i < 10000; i++) { pst.setInt(1, i); // 把一個SQL命令加入命令列表 pst.addBatch(); } // 執行批量更新 pst.executeBatch(); // 語句執行完畢,提交本事務 con.commit();

㈤ Java中如何實現與後台資料庫的連接

用JAVA連接資料庫主要有兩種方式,一是用JDBC-ODBC橋來連接,二是用相關廠商提供的相應驅動程序來連接,首先談談第一種連接。

JDBC-ODBC橋接器是用JdbcOdbc.Class和一個用於訪問ODBC驅動程序的本地庫實現的。對於WINDOWS平台,該本地庫是一個動態連接庫DLL(JDBCODBC.DLL)。

由於JDBC在設計上與ODBC很接近。在內部,這個驅動程序把JDBC的方法映射到ODBC調用上,這樣,JDBC就可以和任何可用的ODBC驅動程序進行交互了。這種橋接器的優點是,它使JDBC目前有能力訪問幾乎所有的資料庫。通行方式如圖所示:

應用程序---JDBC API---JDBC-ODBC---ODBC API---ODBC層---數據源

具體操作方法為:

首先打開控制面板的管理工具,打開數據源(ODBC),在用戶DSN裡面添加數據源(即你要連接的資料庫的名字),在這里假定連接SQL SERVER 2000的GoodsSupply資料庫。名稱填寫你要連接的資料庫的名稱(GoodsSupply),然後逐步設置,如果選用了使用SQL-SERVER密碼認證的話,就要輸入相應的用戶名及密碼連接到資料庫。一路下一步設置完成。

在JAVA裡面編寫程序進行測試,在這里我的程序是讓用戶輸入任意的表名與與列名,把該列的所有數據輸出。源代碼如下:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.*;

public class ODBCBridge {

public static void main(String[] args) {
String url="jdbc:odbc:GoodsSupply";
Statement sm=null;
String command=null;
ResultSet rs=null;
String tableName=null;
String cName=null;
String result=null;
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
try {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //載入驅動
}catch(ClassNotFoundException e){
System.out.println("Can not load Jdbc-Odbc Bridge Driver");
System.err.print("ClassNotFoundException:");
System.err.println(e.getMessage());
}
Connection con=DriverManager.getConnection(url,"USER","PASSWORD"); //使用SQL-SERVER2000認證
DatabaseMetaData dmd=con.getMetaData(); //DMD為連接的相應情況
System.out.println("連接的資料庫:"+dmd.getURL());
System.out.println("驅動程序:"+dmd.getDriverName());
sm=con.createStatement();
System.out.println("輸入表名");
tableName=input.readLine();
while(true) {
System.out.println("輸入列名(為空時程序結束):");
cName=input.readLine();
if(cName.equalsIgnoreCase(""))
break;
command="select "+cName+" from "+tableName;
rs=sm.executeQuery(command); //執行查詢
if(!rs.next())
System.out.println("表名或列名輸入有誤");
else {
System.out.println("查詢結果為:");
do
{
result=rs.getString(cName);
//資料庫語言設置為中文,不用轉換編碼
//result=new String(result.getBytes("ISO-8859-1"),"GB2312");
System.out.println(result);
}while(rs.next());
}
}
}catch(SQLException ex) {
System.out.println("SQLException:");
while(ex!=null) {
System.out.println("Message:"+ex.getMessage());
ex=ex.getNextException();
}
}catch(Exception e) {
System.out.println("IOException");
}
}
}

㈥ 如何實現java與資料庫的連接

JDBC連接資料庫
•創建一個以JDBC連接資料庫的程序,包含7個步驟:
1、載入JDBC驅動程序:
在連接資料庫之前,首先要載入想要連接的資料庫的驅動到JVM(Java虛擬機),
這通過java.lang.Class類的靜態方法forName(String className)實現。
例如:
try{
//載入MySql的驅動類
Class.forName("com.mysql.jdbc.Driver") ;
}catch(ClassNotFoundException e){
System.out.println("找不到驅動程序類 ,載入驅動失敗!");
e.printStackTrace() ;
}
成功載入後,會將Driver類的實例注冊到DriverManager類中。
2、提供JDBC連接的URL
•連接URL定義了連接資料庫時的協議、子協議、數據源標識。
•書寫形式:協議:子協議:數據源標識
協議:在JDBC中總是以jdbc開始
子協議:是橋連接的驅動程序或是資料庫管理系統名稱。
數據源標識:標記找到資料庫來源的地址與連接埠。
例如:(MySql的連接URL)
jdbc:mysql:
//localhost:3306/test?useUnicode=true&characterEncoding=gbk ;
useUnicode=true:表示使用Unicode字元集。如果characterEncoding設置為
gb2312或GBK,本參數必須設置為true 。characterEncoding=gbk:字元編碼方式。
3、創建資料庫的連接
•要連接資料庫,需要向java.sql.DriverManager請求並獲得Connection對象,
該對象就代表一個資料庫的連接。
•使用DriverManager的getConnectin(String url , String username ,
String password )方法傳入指定的欲連接的資料庫的路徑、資料庫的用戶名和
密碼來獲得。
例如:
//連接MySql資料庫,用戶名和密碼都是root
String url = "jdbc:mysql://localhost:3306/test" ;
String username = "root" ;
String password = "root" ;
try{
Connection con =
DriverManager.getConnection(url , username , password ) ;
}catch(SQLException se){
System.out.println("資料庫連接失敗!");
se.printStackTrace() ;
}
4、創建一個Statement
•要執行SQL語句,必須獲得java.sql.Statement實例,Statement實例分為以下3
種類型:
1、執行靜態SQL語句。通常通過Statement實例實現。
2、執行動態SQL語句。通常通過PreparedStatement實例實現。
3、執行資料庫存儲過程。通常通過CallableStatement實例實現。
具體的實現方式:
Statement stmt = con.createStatement() ;
PreparedStatement pstmt = con.prepareStatement(sql) ;
CallableStatement cstmt =
con.prepareCall("{CALL demoSp(? , ?)}") ;
5、執行SQL語句
Statement介面提供了三種執行SQL語句的方法:executeQuery 、executeUpdate
和execute
1、ResultSet executeQuery(String sqlString):執行查詢資料庫的SQL語句
,返回一個結果集(ResultSet)對象。
2、int executeUpdate(String sqlString):用於執行INSERT、UPDATE或
DELETE語句以及SQL DDL語句,如:CREATE TABLE和DROP TABLE等
3、execute(sqlString):用於執行返回多個結果集、多個更新計數或二者組合的
語句。
具體實現的代碼:
ResultSet rs = stmt.executeQuery("SELECT * FROM ...") ;
int rows = stmt.executeUpdate("INSERT INTO ...") ;
boolean flag = stmt.execute(String sql) ;
6、處理結果
兩種情況:
1、執行更新返回的是本次操作影響到的記錄數。
2、執行查詢返回的結果是一個ResultSet對象。
• ResultSet包含符合SQL語句中條件的所有行,並且它通過一套get方法提供了對這些
行中數據的訪問。
• 使用結果集(ResultSet)對象的訪問方法獲取數據:
while(rs.next()){
String name = rs.getString("name") ;
String pass = rs.getString(1) ; // 此方法比較高效
}
(列是從左到右編號的,並且從列1開始)
7、關閉JDBC對象
操作完成以後要把所有使用的JDBC對象全都關閉,以釋放JDBC資源,關閉順序和聲
明順序相反:
1、關閉記錄集
2、關閉聲明
3、關閉連接對象
if(rs != null){ // 關閉記錄集
try{
rs.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
if(stmt != null){ // 關閉聲明
try{
stmt.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
if(conn != null){ // 關閉連接對象
try{
conn.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}

㈦ 怎樣使用java代碼實現資料庫表的自動備份

將MySql中的資料庫導出到文件中 備份
import java.io.*;
import java.lang.*;
public class BeiFen {
public static void main(String[] args) {
// 資料庫導出
String user = "root"; // 資料庫帳號
String password = "root"; // 登陸密碼
String database = "test"; // 需要備份的資料庫名
String filepath = "e:\\test.sql"; // 備份的路徑地址
String stmt1 = "mysqlmp " + database + " -u " + user + " -p"
+ password + " --result-file=" + filepath;
/*
* String mysql="mysqlmp test -u root -proot
* --result-file=d:\\test.sql";
*/
try {
Runtime.getRuntime().exec(stmt1);
System.out.println("數據已導出到文件" + filepath + "中");
}
catch (IOException e) {
e.printStackTrace();
}
}
}

將數據從磁碟上的文本文件還原到MySql中的資料庫
import java.io.*;
import java.lang.*;

/*
* 還原MySql資料庫
* */
public class Recover {

public static void main(String[] args) {

String filepath = "d:\\test.sql"; // 備份的路徑地址
//新建資料庫test
String stmt1 = "mysqladmin -u root -proot create test";
String stmt2 = "mysql -u root -proot test < " + filepath;
String[] cmd = { "cmd", "/c", stmt2 };

try {
Runtime.getRuntime().exec(stmt1);
Runtime.getRuntime().exec(cmd);
System.out.println("數據已從 " + filepath + " 導入到資料庫中");
} catch (IOException e) {
e.printStackTrace();
}
}
}

㈧ 在Java語言中如何實現資料庫的訪問

1.JDBC唄,import java.sql.*;
傳統連接方式
Connection conn = getConnection();//獲得連接
//注意下邊應該拋出個SQLException異常
Statement stat = conn.createStatement();

stat.executeXXX(".........")//執行SQL語句,查詢就是Query,插入,修改這種操作就Update

ResultSet result; //查詢後返回的結果集用這個保存

conn.close//最後別忘了關閉連接

2.當然是Hibernate了,是一種持久層技術,建立相應的實體後可以把資料庫當對象操作,非常方便,具體使用我覺得應該看看書會比較好!

㈨ 如何用Java實現資料庫查詢

import java.sql.*;
public class MSSQLText
{
public static void main(String args[])
{
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind";
String user="sa";//這里替換成你自已的資料庫用戶名
String password="sa";//這里替換成你自已的資料庫用戶密碼
String sqlStr="select CustomerID, CompanyName, ContactName from Customers";
try
{ //這里的異常處理語句是必需的.否則不能通過編譯!
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("類實例化成功!");

Connection con = DriverManager.getConnection(url,user,password);
System.out.println("創建連接對像成功!");

Statement st = con.createStatement();
System.out.println("創建Statement成功!");

ResultSet rs = st.executeQuery(sqlStr);
System.out.println("操作數據表成功!");
System.out.println("----------------!");

while(rs.next())
{
System.out.print(rs.getString("CustomerID") + " ");
System.out.print(rs.getString("CompanyName") + " ");
System.out.println(rs.getString("ContactName"));
}
rs.close();
st.close();
con.close();
}
catch(Exception err){
err.printStackTrace(System.out);
}
}
}

㈩ 怎麼使用JAVA連接資料庫

1、首先我們先建好資料庫,然後建立好程序的目錄,因為是適用於初學者的,所以就建立一個簡單的java project,如圖。

熱點內容
青少年趣味編程社區 發布:2025-07-16 20:35:26 瀏覽:96
小黃車怎麼更改密碼 發布:2025-07-16 20:34:43 瀏覽:189
為什麼國產車氣囊少配置高 發布:2025-07-16 20:18:46 瀏覽:461
pci加密解密控制器驅動 發布:2025-07-16 20:16:18 瀏覽:175
銳龍本編程 發布:2025-07-16 19:35:08 瀏覽:645
初學c語言用什麼軟體 發布:2025-07-16 19:29:12 瀏覽:339
編譯原理實驗分析子程序 發布:2025-07-16 19:28:06 瀏覽:463
長江存儲宿舍有wifi嗎 發布:2025-07-16 19:20:45 瀏覽:872
sqlservertrigger 發布:2025-07-16 19:08:19 瀏覽:400
android中權重 發布:2025-07-16 19:07:26 瀏覽:422