本文目錄一覽:
- 1、如何用java創建mysql資料庫
- 2、java語言如何動態循環創建mysql資料庫基本表,可以實現嗎?
- 3、Java開發工程師如何在Mysql資料庫中創建表
- 4、如何用java創建MYSQL的數據表?
- 5、java 創建MySQL表
如何用java創建mysql資料庫
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=truecharacterEncoding=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(? , ?)}”) ;
java語言如何動態循環創建mysql資料庫基本表,可以實現嗎?
下面是一個簡單的連接MySQL資料庫,並操作資料庫表的例子:
import java.sql.*;
public class TestMysql {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//載入驅動
Class.forName(“com.mysql.jdbc.Driver”);
//創建連接
conn = DriverManager
.getConnection(“jdbc:mysql://localhost/bbs?user=用戶名password=密碼”);
stmt = conn.createStatement();
rs = stmt.executeQuery(“select * from user”);
while (rs.next()) {
String user = rs.getString(1);
String password = rs.getString(2);
System.out.println(“用戶名:” + user + “,” +” 密碼:” + password );
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
當然前提是要把MySQL的連接驅動JAR文件添加到工程里
Java開發工程師如何在Mysql資料庫中創建表
package com.runoob.test;
import java.sql.*;
public class MySQLDemo {
// JDBC 驅動名及資料庫 URL
static final String JDBC_DRIVER = “com.mysql.jdbc.Driver”;
static final String DB_URL = “jdbc:mysql://localhost:3306/RUNOOB”;
// 資料庫的用戶名與密碼,需要根據自己的設置 static final String USER = “root”; static final String PASS = “123456”;
public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{
// 註冊 JDBC 驅動 Class.forName(“com.mysql.jdbc.Driver”);
// 打開鏈接 System.out.println(“連接資料庫…”); conn = DriverManager.getConnection(DB_URL,USER,PASS);
// 執行查詢 System.out.println(” 實例化Statement對象…”); stmt = conn.createStatement(); String sql; sql = “SELECT id, name, url FROM websites”; ResultSet rs = stmt.executeQuery(sql);
// 展開結果集資料庫 while(rs.next()){
// 通過欄位檢索 int id = rs.getInt(“id”); String name = rs.getString(“name”); String url = rs.getString(“url”);
// 輸出數據 System.out.print(“ID: ” + id); System.out.print(“, 站點名稱: ” + name); System.out.print(“, 站點 URL: ” + url); System.out.print(“\n”);
}
// 完成後關閉 rs.close(); stmt.close(); conn.close();
}catch(SQLException se){
// 處理 JDBC 錯誤 se.printStackTrace();
}catch(Exception e){
// 處理 Class.forName 錯誤 e.printStackTrace();
}finally{
// 關閉資源 try{ if(stmt!=null) stmt.close();
}catch(SQLException se2){
}// 什麼都不做 try{ if(conn!=null) conn.close();
}catch(SQLException se){ se.printStackTrace();
}
} System.out.println(“Goodbye!”);
}
}
如何用java創建MYSQL的數據表?
class.forname(“oracle.jdbc.driver.OracleDriver”);//載入資料庫驅動
String url=”jdbc:oracle:thin:@localhost:1521:db_name”;
String sql=”CREATE TABLE table(filed1 varchar2(2),filed2 varchar2(2))”;
Connection conn=DriverManager.getConnection(url,”scott”,”tiger”);//建立資料庫連接
if(!conn.isClose()){
Statement stmt = conn.createStatement();
stmt.executeUPDATE(sql); //建立一個表
}
java 創建MySQL表
create table UserInfo( 創建 表 表名 這裡create table是固定寫法,表名自己起
id int parmary key, 列名,數據類型。parmary key表示該列為主鍵列
name varchar(20) not null, 列名,數據類型(數據長度)。not null表示該列不允許為空
age int not null 這個同上
)
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/307416.html