① javaEE開發中,資料庫建表欄位都採用varchar2()類型可以嗎
在JavaEE開發中,資料庫建表欄位不建議全部採用varchar2類型。原因如下:
性能影響:
數據類型一致性:
規范操作:
綜上所述,在JavaEE開發中,資料庫建表欄位的選擇應根據數據的特性、業務需求以及預期的性能目標進行綜合考慮。不建議全部採用varchar2類型,而應選擇最適合的數據類型以優化資料庫性能。
② 使用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
基本就這樣啦!不過那個界面的設計代碼就不給你啦!
③ eclipse怎麼建立mysql資料庫表的java文件
方法/步驟
1.前邊的事例是把資料庫的驅動,連接,用戶名和密碼都寫在了類中,耦合性太高,當我們資料庫變更或者資料庫類型更換後,需要去重新更改代碼,很不方便。
解決的方法:把資料庫的驅動,連接,用戶名和密碼寫在配置文件中,通過讀取配置文件的方式進行代碼編寫,而以後如果資料庫變更直接修改配置文件即可!
2.在工程中右鍵新建file,命名為jdbc.properties
3.創建完畢如圖:
4.在jdbc.properties文件中輸入如下信息,分別是資料庫的驅動,連接,用戶名和密碼
5.新建JdbcTest2.java類
6.輸入如下代碼:
7.代碼說明:
這段代碼是讀取配置文件,把配置文件中的各個項通過名稱讀取出來
8.這段代碼是通過反射來創謹歲建Driver對象,反射就是類的實例化
9.在主函數中輸入如下凱蔽,測試方法
10.運行之後的結果如下,表示連接成功!盯晌州