如何使用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();
}
}
还有问题可以继续联系
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();
}
}
还有问题可以继续联系
追问
如果我是想要显示到网页中呢?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询