如何在netbeans中创建javaDb? [英] How to create a javaDb within netbeans?
问题描述
大家好.我正在通过netbeans开发桌面应用程序.我需要创建一个数据库.我已经尝试了甲骨文.但是我无法成功.所以我搜索并决定了Netbeans中的JavaDB.我已经使用指南来创建数据库.现在,我想通过接口插入数据.我有一个包含2个文本框(用户名和用户密码)和一个组合框(用户角色)的表单.我做了一些事情,但是没有用.你能帮我吗?
Hello everyone. I am developing a desktop application via netbeans. I need to create a database. I hava tried oracle. However I couldn''t become successful. so I searched and decided on JavaDB which is within Netbeans. I have used a guide to create the database. Now I want to insert data via interface. I have a form which includes 2 textboxes (username and user password) and a combobox (user role). I have done something but does not work. Could you help me ?
try
{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
}catch(ClassNotFoundException e)
{
System.out.println(e);
}
try {
//1.Kısım Baglantı Saglıyoruz
this.host = "jdbc:derby://localhost:1527/EMRE";
this.Name = "emre";
this.Pass = "2561";
Connection con = DriverManager.getConnection(host,Name,Pass);
//2.Kısım Statement Olusturuyorum
Statement cumle = con.createStatement();
String query = "INSERT INTO P_LOGIN VALUES('"+txtAddUserName.getText()+"','"+txtAddUserPassWord.getText()+"','"+cmbAddUserRole.getSelectedItem().toString()+"')";
ResultSet rs = cumle.executeQuery(query);
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
}
这是我的添加按钮代码.我在哪里做错了?请向我展示一种方法.
This is my ADD button code. Where am I doing wrong? Please show me a way.
推荐答案
在这里,您正在使用createStatement()方法,因为您正在向数据库中插入值,因此需要使用prepareStatement
方法:-
Here you are using createStatement() method, because you are inserting values to database you need to useprepareStatement
method:-
PreparedStatement ps = scon.prepareStatement("INSERT INTO P_LOGIN VALUES(?,?,?)");
ps.setString(1, txtAddUserName.getText());
ps.setString(2, txtAddUserPassWord.getText());
ps.setString(3, cmbAddUserRole.getSelectedItem().toString());
int i = ps.executeUpdate();
if (i > 0)
{
//do your stuff
}
要处理JavaDB,请通过以下链接:-
使用Java DB(Derby)数据库 [
For dealing with JavaDB, Please go through this link:-
Working with the Java DB (Derby) Database[^]
Good luck.
我和Rajesh在一起-德比规则! +5.
但我想您的Google坏了,您正在搜索本教程:
使用Java DB(Derby)数据库 [
I''m with Rajesh - Derby rules! +5 for that.
But I guess your google is broken and you are searching for this tutorial:
Working with the Java DB (Derby) Database[^]
try {
//1.Kısım Baglantı Saglıyoruz
this.host = "jdbc:derby://localhost:1527/EMRE";
this.Name = "emre";
this.Pass = "2561";
Connection con = DriverManager.getConnection(host,Name,Pass);
//2.Kısım Statement Olusturuyorum
Statement cumle = con.createStatement();
cumle.executeUpdate("INSERT INTO P_LOGIN " + " (SIFRE, KULLANICI_ADI, ROLE)" + " VALUES ('"+txtAddUserPassWord.getText()+"','"+txtAddUserName.getText()+"','"+cmbAddUserRole.getSelectedItem()+"')");
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
}
感谢TorstenH.和Rajesh.
Thanks to TorstenH. and Rajesh.
这篇关于如何在netbeans中创建javaDb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!