java動態創建資料庫
Java要連接資料庫,那麼首先你必須安裝mysql資料庫。
安裝好mysql之後,安裝JDK了。
安裝好JDK之後,就是安裝Eclipse了,要支持JDK版本,Eclipse安裝的時候會自動去找JDK安裝位置的,解壓版的Eclipse,就要配置eclipse.ini文件了,將對應的JDK配置好,這些已經准備就緒的時候,就到mysql中創建資料庫和表。
先創建資料庫:
CREATE DATABASE SCUTCS;
接著,創建表:
CREATE TABLE STUDENT
(
SNO CHAR(7) NOT NULL,
SNAME VARCHAR(8) NOT NULL,
SEX CHAR(2) NOT NULL,
BDATE DATE NOT NULL,
HEIGHT DEC(5,2) DEFAULT 000.00,
PRIMARY KEY(SNO)
);
然後插入數據,可以用SQL語句insert into <表名> values (value1, value2, ...);
編寫.java文件來演示一下如何訪問MySQL資料庫。
import java.sql.*;
public class JDBCTest {
public static void main(String[] args){
// 驅動程序名 String driver = "com.mysql.jdbc.Driver";
// URL指向要訪問的資料庫名scutcs String url = "jdbc:mysql://127.0.0.1:3306/scutcs";
// MySQL配置時的用戶名 String user = "root"; // MySQL配置時的密碼 String password = "root";
try { // 載入驅動程序 Class.forName(driver);
// 連續資料庫 Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed()) System.out.println("Succeeded connecting to the Database!");
// statement用來執行SQL語句 Statement statement = conn.createStatement();
// 要執行的SQL語句 String sql = "select * from student";
// 結果集 ResultSet rs = statement.executeQuery(sql);
while(rs.next()) // 選擇sname這列數據 name = rs.getString("sname
// 輸出結果 System.out.println(rs.getString("sno") + "\t" + name); }
rs.close(); conn.close();
} catch(ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!"); e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} } }
㈡ 怎樣用java代碼動態生成資料庫表
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection("資料庫url","帳號","密碼");
state=conn.createStatement();
state.executeUpdate("create 建表語句");
state.executeUpdate("insert 插入數據")------>插入的值由頁面獲得,注意字元串拼接。
然後就是關閉連接,state.close();conn.close();
核心代碼就是這些,具體應用你可以多寫幾個方法(增刪改查),都是類似的,注意異常的處理,關閉連接最好在finally中進行。
㈢ 通過java代碼如何實現對mysql資料庫進行創建新的資料庫的操作
1 import java.sql.Connection;
2 import java.sql.DriverManager;
3 import java.sql.ResultSet;
4 import java.sql.SQLException;
5 import java.sql.Statement;
6
7 public class CreateDataSource {
8
9 /**
10 * @param args
11 */
12 public static void main(String[] args) {
13 // TODO Auto-generated method stub
14 String database = "test2";
15 new CreateDataSource().getConn(database);
16 }
17
18 String mysqlDriver = "com.mysql.jdbc.Driver";
19 String url = "jdbc:mysql://localhost:3306/test1";
20 String newUrl = "jdbc:mysql://localhost:3306/";
21 String username = "root";
22 String password = "root";
23 Connection conn = null;
24 Connection newConn = null;
25
26 public Connection getConn(String database) {
27
28 try {
29 Class.forName(mysqlDriver);
30 } catch (ClassNotFoundException e) {
31 // TODO Auto-generated catch block
32 e.printStackTrace();
33 }
34 try {
35 String tableSql = "create table t_user (username varchar(50) not null primary key,"
36 + "password varchar(20) not null ); ";
37 String databaseSql = "create database " + database;
38
39 conn = DriverManager.getConnection(url, username, password);
40 Statement smt = conn.createStatement();
41 if (conn != null) {
42 System.out.println("資料庫連接成功!");
43
44 smt.executeUpdate(databaseSql);
45
46 newConn = DriverManager.getConnection(newUrl + database,
47 username, password);
48 if (newConn != null) {
49 System.out.println("已經連接到新創建的資料庫:" + database);
50
51 Statement newSmt = newConn.createStatement();
52 int i = newSmt.executeUpdate(tableSql);//DDL語句返回值為0;
53 if (i == 0) {
54 System.out.println(tableSql + "表已經創建成功!");
55 }
56 }
57 }
58
59 } catch (SQLException e1) {
60 // TODO Auto-generated catch block
61 e1.printStackTrace();
62 }
63 return conn;
64 }
65 }
㈣ 如何運用java在mysql中創建資料庫
JDBC 獲取連接有這樣一個方法
(Stringurl,
Stringuser,Stringpassword)throwsSQLException
url可以是資料庫主機的ip地址,這里不需要指定庫名;
分以下步驟創建資料庫,但是對應的用戶應該是高許可權的用戶,比如說root;
conn=DriverManager.getConnection(DB_URL,USER,PASS);
stmt=conn.createStatement();
Stringsql="CREATEDATABASESTUDENTS";
stmt.executeUpdate(sql);