當前位置:首頁 » 網路資訊 » eclipse怎樣讀取資料庫
擴展閱讀
怎樣將抖音發在桌面上 2024-09-19 08:56:20

eclipse怎樣讀取資料庫

發布時間: 2022-01-19 00:09:03

A. 怎麼從my eclipse 用代碼將資料庫中的數據中讀出來

1、myeclipse 只是一個開發工具
2、java開發,需要導入連接資料庫的驅動包,就是連接資料庫的jar包,最基礎的就是jdbc連接,不同資料庫有不同的jar包

B. eclipse怎樣用mysql資料庫

題主,你好.具體請參看下方代碼

importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Connection;
importjava.sql.Statement;


publicclassMysqlDemo{
publicstaticvoidmain(String[]args)throwsException{
Connectionconn=null;
Stringsql;
//MySQL的JDBCURL編寫方式:jdbc:mysql://主機名稱:連接埠/資料庫的名稱?參數=值
//避免中文亂碼要指定useUnicode和characterEncoding
//執行資料庫操作之前要在資料庫管理系統上創建一個資料庫,名字自己定,
//下面語句之前就要先創建javademo資料庫
Stringurl="jdbc:mysql://localhost:3306/javademo?"
+"user=root&password=root&useUnicode=true&characterEncoding=UTF8";

try{
//之所以要使用下面這條語句,是因為要使用MySQL的驅動,所以我們要把它驅動起來,
//可以通過Class.forName把它載入進去,也可以通過初始化來驅動起來,下面三種形式都可以
Class.forName("com.mysql.jdbc.Driver");//動態載入mysql驅動
//or:
//com.mysql.jdbc.Driverdriver=newcom.mysql.jdbc.Driver();
//or:
//newcom.mysql.jdbc.Driver();

System.out.println("成功載入MySQL驅動程序");
//一個Connection代表一個資料庫連接
conn=DriverManager.getConnection(url);
//Statement裡面帶有很多方法,比如executeUpdate可以實現插入,更新和刪除等
Statementstmt=conn.createStatement();
sql="createtablestudent(NOchar(20),namevarchar(20),primarykey(NO))";
intresult=stmt.executeUpdate(sql);//executeUpdate語句會返回一個受影響的行數,如果返回-1就沒有成功
if(result!=-1){
System.out.println("創建數據表成功");
sql="insertintostudent(NO,name)values('2012001','陶偉基')";
result=stmt.executeUpdate(sql);
sql="insertintostudent(NO,name)values('2012002','周小俊')";
result=stmt.executeUpdate(sql);
sql="select*fromstudent";
ResultSetrs=stmt.executeQuery(sql);//executeQuery會返回結果的集合,否則返回空值
System.out.println("學號 姓名");
while(rs.next()){
System.out
.println(rs.getString(1)+" "+rs.getString(2));//入如果返回的是int類型可以用getInt()
}
}
}catch(SQLExceptione){
System.out.println("MySQL操作錯誤");
e.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
}finally{
conn.close();
}

}

}

C. 誰知道eclipse中如何讀取MySQL資料庫表中的一行信息並保存列印出來

用循環保存
int index =0;
while(rs.next){
Object obj = rs.get(index ++);
}

D. Eclipse如何連接資料庫

  • 1.在新建的Project中右鍵新建Floder

  • 12

    12.說明:這個是使用Driver連接資料庫的,而通常開發中使用的是DriverManager或資料庫連接池,這個僅作為理解資料庫連接事例使用。

E. Eclipse怎樣連接並打開oracle等資料庫

Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://"+host+"/"+dbname,username,password);
Statement Stmt=conn.createStatement();
ResultSet rs=Stmt.executeQuery(sql);
這是基本的資料庫操作方法
大概流程就是載入驅動類,創建連接,執行資料庫操作,關閉

F. Eclipse怎麼從資料庫中獲取數據

是要用eclipse中的資料庫連接插件連接資料庫用戶,還是要用代碼獲取資料庫連接從而讀取數據?

G. eclipse怎樣連接mysql資料庫管理表

工具:

eclipse

步驟



1、導入jdbc驅動包。點擊菜單欄中的Windows→preferences。


H. 怎樣用eclipse鏈接資料庫

步驟如下:
1、打開Eclipse。
2、點擊菜單欄的「Window—>Show View—>Data Source Explorer」。
3、在「Data Source Explorer」裡面的「Database Connections」單擊滑鼠右鍵,選擇「New」。
4、在彈出窗口上選擇資料庫類型,然後點「Next」。
5、添加好驅動,填好各項配置。
6、完成後,點「Test Connection」,提示成功即可點「Finish」完成配置。

I. 如何使用eclipse調用資料庫中的內容,並將數據顯示出來具體步驟和代碼··

我用的資料庫是mysql,下載這個東東mysql-connector-java-5.1.15.zip解壓把mysql-connector-java-5.1.15-bin.jar導入到你要連接資料庫的項目中(應該知道怎麼導入吧!)然後就是代碼,以下代碼是插入資料庫的例子

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import java.sql.*;

public class Login2 {

private Connection connection;
private JButton button1;
private JFrame frame;
private JLabel nameLabel,pwdLabel;
private JTextField nameTA,pwdTA;
private JPanel panel;
// private Statement stat;
private ResultSet rs;
public Login2()
{
String url = "jdbc:mysql://localhost:3306/(此處填寫你創建的資料庫名字)";
String username = "(此處填寫你的資料庫用戶,例如root)";
String password = "(此處填寫你的資料庫安裝時設置的密碼)";
//載入驅動程序以連接資料庫
try {
Class.forName( "org.gjt.mm.mysql.Driver" );
connection = DriverManager.getConnection(
url, username, password );
}
//捕獲載入驅動程序異常
catch ( ClassNotFoundException cnfex ) {
System.err.println(
"裝載 JDBC/ODBC 驅動程序失敗。" );
cnfex.printStackTrace();
System.exit( 1 ); // terminate program
}
//捕獲連接資料庫異常
catch ( SQLException sqlex ) {
System.err.println( "無法連接資料庫" );
sqlex.printStackTrace();
System.exit( 1 ); // terminate program
}
frame = new JFrame();

panel = new JPanel();
panel.setLayout(new GridLayout(3,2));
nameLabel = new JLabel("user");
pwdLabel = new JLabel("password");
nameTA = new JTextField();
pwdTA = new JTextField();
button1 = new JButton("insert");

button1.addActionListener(new ActionListener()
{

public void actionPerformed(ActionEvent e)
{
String str1 = nameTA.getText();
String str2 = pwdTA.getText();
String str = "insert into user values('"+str1+"','"+str2+"')";

try {
// Statement stat = null;
PreparedStatement pstmt = connection.prepareStatement(str);

pstmt.executeUpdate();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null, "yes");

}
});

panel.add(nameLabel);
panel.add(nameTA);
panel.add(pwdLabel);
panel.add(pwdTA);
panel.add(button1);

frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,100);
frame.setVisible(true);

}

/**
* @param args
*/
public static void main(String[] args) {

Login2 l = new Login2();

}

}
還有問題可以繼續聯系

J. eclipse怎麼調用資料庫

要使用對應資料庫的jar包,通過jdbc來訪問資料庫