『壹』 java中創建oracle表問題
應該是多了個分號吧
preparedStatement里邊的SQL語句不需要分號標志結束。
『貳』 java程序怎樣執行sql文件創建oracle表
代碼樣例:
String sql = "create table tablename(id number, title varchar2(20), intro varchar2(200), time timestamp)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.execute();
解析:
建表語句與oracle建表語句相同。主要的點是java程序與資料庫連接及數據交互的過程代碼。
PreparedStatement與Statement區別:
statement每次執行sql語句,相關資料庫都要執行sql語句的編譯,preparedstatement是預編譯得,preparedstatement支持批處理。
『叄』 如何用java獲取一個oracle表的創建的sql語句
java獲取一個oracle表的創建的sql語句:
直接通過jdbc調用:SELECT DBMS_METADATA.GET_DDL('TABLE','EMP','SCOTT') FROM DUAL;就可以了。
返回結果如下:
DBMS_METADATA.GET_DDL('TABLE','EMP','SCOTT')
--------------------------------------------------------------------------------
CREATE TABLE "SCOTT"."EMP"
( "EMPNO" NUMBER(4,0),
"ENAME" VARCHAR2(10),
"JOB" VARCHAR2(9),
"MGR" NUMBER(4,0),
"HIREDATE" DATE,
"SAL" NUMBER(7,2),
"COMM" NUMBER(7,2),
"DEPTNO" NUMBER(2,0),
CONSTRAINT "PK_EMP" PRIMARY KEY ("EMPNO")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "USERS" ENABLE,
CONSTRAINT "FK_DEPTNO" FOREIGN KEY ("DEPTNO")
REFERENCES "SCOTT"."DEPT" ("DEPTNO") ENABLE
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "USERS"
『肆』 怎樣通過java代碼執行sql文件創建oracle資料庫表
先用jdbc連接資料庫,然後再執行sql語句
jdbc是java連接資料庫來的,你要自己查代碼
『伍』 怎麼用java在oracle資料庫中建表
jdbc:oracle:thin:@127.0.0.1:1521:資料庫名字
這個就是你的地址。
其中127.0.0.1是本地地址,如果你的資料庫在其他伺服器只需要修改為對應伺服器地址就ok
『陸』 使用java連接oracle資料庫的詳細步驟,以及怎樣在oracle資料庫里建庫建表,和用戶名及許可權的設置
你按照我以下的步驟就可以建立java跟oracle的鏈接:
(1)首先要安裝oracle資料庫(這是廢話,不過這個過程中你可以設置用戶名機密碼他的許可權相當於管理員),然後啟動查詢分析器再用 great database databasename(數據 庫的名稱)的命令建立資料庫,之後就是要建立資料庫的表,建表的命令如下(我給你的例子是建立一個學生表):
usr database/*你剛才所建立的資料庫的名稱,一定要相同,那麼你就是再這個資料庫中建立了這個表*/
CREATE TABLE stu
(
sno char(10) NOT NULL /*學號欄位*/
CONSTRAINT PK_sno PRIMARY KEY CLUSTERED,/*主鍵約束*/
sname char(8) NOT NULL, /*姓名欄位*/
sex char(2) NULL, /*性別欄位*/
native int NULL, /*籍貫*/
birthday varchar(20) NULL,/*學生出生日期*/
dno char(6) NULL,/*學生所在院系編號(外鍵)*/
spno char(8) NULL,/*專業代碼(外鍵)*/
classno char(4) NULL,/*班級號*/
entime char(4) NULL,/*學生入校時間*/
home varchar(40) NULL,/*學生家庭住址*/
tel varchar(40) NULL/*學生聯系電話*/
)
這樣你的資料庫和相應的表就建成了,如果你需要對資料庫的許可權進行設置那麼就涉及到角色的賦予或者你安裝oracle時需要進行設置的用戶明及密碼,這塊說來就話長啦!如果你只是學習java和資料庫的鏈接,那麼這個可以暫時放一邊,如果你非得想知道那麼你需要系統學習資料庫的知識。我這里就不跟你介紹了。建立完表之後就需要對表插入數據(插入數據可以用java編程,用自己設置的軟體插入數據也可以用資料庫的查詢分析氣用sql語句插入)
(2)這一步也是java跟資料庫鏈接的關鍵,在你安裝了資料庫的那台pc機或者伺服器注冊數據源步驟:進入你電腦的控制面板——管理工具——數據源——系統DNS(選中)——添加(在這裡面有你要添加的數據源添加microsoft DOBC for Orccle,再這里點擊完成後會彈出一個對話框,要你填寫數據源的名稱這個名稱一定要記住,java鏈接程序編程時需要用到這個名稱,還有要填伺服器的名稱,這個名稱需要你的伺服器名稱,如果你是單台pc機實驗,那麼在你資料庫登錄的界面那個伺服器名稱就可以了,然後點擊下去進行必要的設置就可以了),這樣我們對資料庫部分的工作已經完成啦!接下來就是完成java的編程部分。
(3)這里就是java的編程部分,這里我給了你一個我從教材弄來的編好並調試成功的程序(當然這跟你自己建立的資料庫是相關的):
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class add extends JFrame {
private StudentUI userInterface;
private JButton clearButton, writeButton;
// 載入啟動程序和建立資料庫的地址,遠程和對本機的資料庫載入是不一樣得,這里給你一個對本機資料庫的操作
static final String JDBC_DRIVER = "("oracle.jdbc.driver.OracleDriver";
static final String DATABASE_URL = "oracle.jdbc.driver:剛才叫你記住的那個數據源的名字";
// declare Connection and Statement for accessing
// and querying database
private Connection connection;
private Statement statement;
String sqlString ;
//set up column names
String names[] = { "學 號","姓 名","性 別","年 齡","所 在 系"};
// set up GUI
public Add()
{
super( "Add a record of students" );
initialize(); //connect to database
// create instance of reusable user interface
userInterface = new StudentUI( names ); // four textfields
getContentPane().add( userInterface, BorderLayout.CENTER );
// configure button doTask1 for use in this program
writeButton = userInterface.getDoTask1Button();
writeButton.setText( "保存" );
// register listener to call addRecord when button pressed
writeButton.addActionListener(
// anonymous inner class to handle writeButton event
new ActionListener() {
// call addRecord when button pressed
public void actionPerformed( ActionEvent event )
{
addRecord();
}
} // end anonymous inner class
); // end call to addActionListener
// configure button doTask2 for use in this program
clearButton = userInterface.getDoTask2Button();
clearButton.setText( "清除" );
// register listener to call userInterface clearFields() when button pressed
clearButton.addActionListener(
// anonymous inner class to handle clearButton event
new ActionListener() {
// call userInterface clearFields() when button pressed
public void actionPerformed( ActionEvent event )
{
userInterface.clearFields();
}
} // end anonymous inner class
); // end call to addActionListener
// register window listener to handle window closing event
addWindowListener(
// anonymous inner class to handle windowClosing event
new WindowAdapter() {
// add current record in GUI to file, then close file
public void windowClosing( WindowEvent event )
{
terminate(); //close databse
}
} // end anonymous inner class
); // end call to addWindowListener
setSize( 300, 200 );
setVisible( true );
} // end of constructor
// connect to database
public void initialize()
{
try {
Class.forName( JDBC_DRIVER );
// establish connection to database
connection = DriverManager.getConnection( DATABASE_URL,"sa",null );
// create Statement for querying database
statement = connection.createStatement();
}
catch ( SQLException sqlException ) {
JOptionPane.showMessageDialog( null, sqlException.getMessage(),
"Database Error", JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
}
// detect problems loading database driver
catch ( ClassNotFoundException classNotFound ) {
JOptionPane.showMessageDialog( null, classNotFound.getMessage(),
"Driver Not Found", JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
}
} // end method openFile
// close database
public void terminate()
{
try {
statement.close();
connection.close();
}
// handle exceptions closing statement and connection
catch ( SQLException sqlException ) {
JOptionPane.showMessageDialog( null,
sqlException.getMessage(), "Database Error",
JOptionPane.ERROR_MESSAGE );
System.exit( 1 );
}
} // end method
// add record to file
public void addRecord()
{
String fieldValues[] = userInterface.getFieldValues();
// if sno field value is not empty
if ( ! fieldValues[ StudentUI.SNO ].equals( "" ) ) {
// output values to student
try {
int numberAge = Integer.parseInt(
fieldValues[ StudentUI.SAGE ] );
//define string for sql insert statement
String sqlInsert = "INSERT INTO student " +
"VALUES ('" +
fieldValues[0] + "', '" +
fieldValues[1] +"', '"+
fieldValues[2]+ "', "
+numberAge+",'"+fieldValues[4] + "')";
int result = statement.executeUpdate(sqlInsert);
if (result!=0) {
userInterface.clearFields();
JOptionPane.showMessageDialog( this,
"Inserted sucess!", "Insert Result",
JOptionPane.INFORMATION_MESSAGE );
}
} // end try
// process invalid age number
catch ( NumberFormatException formatException ) {
JOptionPane.showMessageDialog( this,
"Bad age number ", "Invalid Number Format",
JOptionPane.ERROR_MESSAGE );
}
// process exceptions from file output
catch (SQLException ee)
{ System.out.println(ee); }
} //end of if sno field value is not empty
else //if sno field value is empty
JOptionPane.showMessageDialog( this,
"Bad sno number ", "Invalid Number Format",
JOptionPane.ERROR_MESSAGE );
} // end method addRecord
public static void main( String args[] )
{
new AddStudentFrame();
}
} // end AddStudentFrame class
基本就這樣啦!不過那個界面的設計代碼就不給你啦!