如何在java中将文本字段中的值插入到数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19357983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert values from textfields into database in java
提问by Komal Daoud
I want to insert values from jtextfields into my database. but following error is occured.. Access denied for user ''@'localhost' to database 'employee' any solution?
我想将 jtextfields 中的值插入到我的数据库中。但发生以下错误.. 用户 ''@'localhost' 对数据库 'employee' 的访问被拒绝 任何解决方案?
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class DataEntry implements ActionListener{
Connection conn = null;
Statement stmt = null;
// Database credentials
//Create and set up the window.
JFrame mainframe=new JFrame("Database Operation");
JPanel mainpanel=new JPanel();
JButton insertbtn=new JButton("Insert");
JButton viewbtn=new JButton("View");
JLabel label1=new JLabel("ID: ");
JLabel label2=new JLabel("Age: ");
JLabel label3=new JLabel("");
JFrame insertframe=new JFrame("Insert into Database");
JPanel insertpanel=new JPanel();
JLabel idlabel=new JLabel(" id");
JTextField idtext=new JTextField(20);
JLabel agelabel=new JLabel(" age");
JTextField agetext=new JTextField(20);
JButton insertsubmit=new JButton("Submit");
JFrame viewframe=new JFrame("View Database Data");
JPanel viewpanel=new JPanel();
Vector datacol=new Vector();
Vector datarow=new Vector();
JTable datatable;
JScrollPane viewdata;
public void init()
{
mainwindow();
}
public void mainwindow()
{
mainpanel.setBackground(Color.GRAY);
mainpanel.setLayout(null);
insertbtn.setBounds(300, 300, 100, 30);
viewbtn.setBounds(800, 300, 100, 30);
mainpanel.add(insertbtn);
mainframe.add(mainpanel);
mainframe.setSize(1350, 700);
mainframe.setVisible(true);
insertbtn.addActionListener(this); }
public void insertwindow()
{
insertpanel.setBackground(Color.GRAY);
idlabel.setForeground(Color.WHITE);
agelabel.setForeground(Color.WHITE);
insertpanel.setLayout(null);
idlabel.setBounds(400, 150, 100, 20);
idtext.setBounds(550, 150, 150, 30);
agelabel.setBounds(400, 200, 100, 20);
agetext.setBounds(550, 200, 150, 30);
insertsubmit.setBounds(490, 300, 100, 30);
insertpanel.add(idlabel);
insertpanel.add(idtext);
insertpanel.add(agelabel);
insertpanel.add(agetext);
insertpanel.add(insertsubmit);
insertframe.add(insertpanel);
insertframe.setSize(1350, 700);
insertframe.setVisible(true);
insertsubmit.addActionListener(this);
insertframe.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
mainframe.setVisible(true);
}
}
);
}
public void viewwindow()
{
datacol.removeAllElements();
datarow.removeAllElements();
OpenDatabase();
try
{
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select id,age from employee");
ResultSetMetaData rms=rs.getMetaData();
int cols=rms.getColumnCount();
for(int i=1;i<=cols;i++)
{
datacol.addElement(rms.getColumnName(i));
}
while(rs.next())
{
Vector row=new Vector(cols);
for(int i=1;i<=cols;i++)
{
row.addElement(rs.getObject(i));
}
datarow.addElement(row);
}
datatable=new JTable(datarow,datacol);
viewdata=new JScrollPane(datatable);
}
catch(Exception e)
{
JOptionPane.showConfirmDialog(null, "Problem in Database connectivity or Data", "Result", JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);
}
viewpanel.setBackground(Color.GRAY);
viewpanel.setLayout(null);
viewdata.setBounds(20, 50, 1250, 600);
viewpanel.add(viewdata);
viewframe.add(viewpanel);
viewframe.setSize(1350, 700);
viewframe.setVisible(true);
viewframe.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
mainframe.setVisible(true);
}
});}
public void insertdata(String id, String age)
{
try
{
Statement st=conn.createStatement();
st.executeUpdate("insert into employee values("+id+",'"+age+")");
JOptionPane.showConfirmDialog(null, "Your Data Has been Inserted", "Result", JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE);
}
catch(Exception e)
{
JOptionPane.showConfirmDialog(null, "Problem in Database connectivity or Data", "Result", JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);
}
}
public void OpenDatabase()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost/employee");
}
catch(Exception e)
{
System.out.println(e);
}
}
public void CloseDatabase()
{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==insertbtn)
{
mainframe.setVisible(false);
insertwindow();
}
if(arg0.getSource()==viewbtn)
{
mainframe.setVisible(false);
viewwindow();
}if(arg0.getSource()==insertsubmit)
{
String id=idtext.getText();
String age=agetext.getText();
try
{
OpenDatabase();
insertdata(id,age);
CloseDatabase();
}
catch(Exception e)
{
System.out.println(e);
}
idtext.setText("");
agetext.setText("");}}
public static void main(String args[])
{
DataEntry dm=new DataEntry();
dm.init();
}
}
回答by justadeveloper
You have problem with
你有问题
conn=DriverManager.getConnection("jdbc:mysql://localhost/employee");
DriverManager.getConnection()
' parameters setting should be like
DriverManager.getConnection()
' 参数设置应该是这样的
DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","username", "password");
If your mysql have default settings and you use root user it should be as
如果你的 mysql 有默认设置并且你使用 root 用户,它应该是
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee","root", "your password");
回答by AJ.
Give user id and password in getConnection method
在 getConnection 方法中提供用户 ID 和密码
conn=DriverManager.getConnection("jdbc:mysql://localhost/employee","root","password");
"root" as user id of my sql(As in yor system) and "password" as password of my sql (As in yor system)
“root”作为我的 sql 的用户 ID(在你的系统中)和“密码”作为我的 sql 的密码(在你的系统中)
回答by Kuldeep Choudhary
Use user name & password of your mysql database in getConnection method
在 getConnection 方法中使用 mysql 数据库的用户名和密码
conn=DriverManager.getConnection("jdbc:mysql://localhost/employee");
as
作为
conn=DriverManager.getConnection("jdbc:mysql://localhost/employee","root","password");