java.lang.ClassNotFoundException:org.apache.derby.jdbc.ClientDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at iir.connection.main(connection.java:35)
2014年11月29日下午5:03:01 iir.connection main
SEVERE:null
java.sql.SQLException:没有为jdbc找到合适的驱动程序:derby:// localhost:1527 / bala
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at iir.connection.main(connection.java:40)
线程“main”中的异常java.lang.NullPointerException
at iir.connection.main(connection.java:44)
答案 0 :(得分:0)
由于您尚未共享代码,因此很难理解您的要求。不过,我正在分享一些示例代码,可以帮助您排除故障:
使用的数据库:Derby
要安装和配置数据库,请参阅链接:https://db.apache.org/derby/papers/DerbyTut/install_software.html#derby_configure
定义属性文件:Database.properties
url=jdbc:derby:myDerbyDB
driver=org.apache.derby.jdbc.EmbeddedDriver
如果需要修改数据库,这将帮助您轻松修改代码。
测试与数据库的连接:Connect.java
package com.stackoverflow.test.Derby;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;
public class Connect
{
public static Connection con;
static String url,driver;
public static void connectMe() throws ClassNotFoundException, SQLException
{
ResourceBundle resource = ResourceBundle.getBundle("database");
driver=resource.getString("driver");
url=resource.getString("url");
Class.forName(driver);
con = DriverManager.getConnection(url);
if(con!=null)
System.out.println("Connection Established!");
}
}
创建表格:TableCreation.java
package com.stackoverflow.test.Derby;
import java.sql.SQLException;
import java.sql.Statement;
public class TableCreation
{
public static void main(String args[]) throws ClassNotFoundException, SQLException
{
Connect.connectMe();
Statement st = Connect.con.createStatement();
String query = "CREATE TABLE myJavaTable (ID INT NOT NULL GENERATED ALWAYS AS IDENTITY CONSTRAINT ID_PK PRIMARY KEY, DATA VARCHAR(20) NOT NULL) ";
boolean val = st.execute(query);
if (val == false) {
System.out.println("Creation Successfull!");
} else {
System.out.println("Creation not successfull!");
}
Connect.con.close();
}
}
将数据插入表格:PreparedStatementInsert.java
package com.stackoverflow.test.Derby;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.*;
public class PreparedStatementInsert
{
public static void main(String args[]) throws ClassNotFoundException, SQLException, IOException
{
Connect.connectMe();
String query;
Statement st = Connect.con.createStatement();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
query="Insert into myJavaTable(DATA) Values(?)";
PreparedStatement ps = Connect.con.prepareStatement(query);
System.out.print("Enter the number of records to be inserted: ");
int n = Integer.parseInt(in.readLine());
for(int i = 1; i<=n; i++)
{
System.out.print("Enter the Data: ");
ps.setString(1, in.readLine());
ps.executeUpdate();
System.out.println("One record inserted!");
}
TableRetrieve.main(args);
Connect.con.close();
}
}
显示表格内容:TableRetrieve.java
package com.stackoverflow.test.Derby;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TableRetrieve
{
public static void main(String args[]) throws ClassNotFoundException, SQLException
{
Connect.connectMe();
Statement st = Connect.con.createStatement();
ResultSet rSet = st.executeQuery("Select * from myJavaTable");
while(rSet.next())
{
System.out.print("ID: "+rSet.getString(1));
System.out.println("\tName: "+rSet.getString(2));
}
Connect.con.close();
}
}
更新表格的内容:TableUpdate.java
package com.stackoverflow.test.Derby;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TableUpdate
{
public static void main(String args[]) throws ClassNotFoundException, SQLException, IOException
{
Connect.connectMe();
String query;
boolean flag = false;
Statement st = Connect.con.createStatement();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the ID of the record to be updated");
int userID = Integer.parseInt(in.readLine());
query = "Select * from myJavaTable";
ResultSet rSet = st.executeQuery(query);
while(rSet.next())
{
int id = rSet.getInt("ID");
if(userID==id)
{
System.out.print("Enter the new data: ");
String str = in.readLine();
query="Update myJavaTable set Data='"+str+"' where ID="+id;
int exe = st.executeUpdate(query);
flag = true;
break;
}
}
if(flag==true)
System.out.println("Updation successfull!");
else
System.out.println("Record not found!");
System.out.println("Table after updation is as follows:");
TableRetrieve.main(args);
Connect.con.close();
}
}
删除创建的表:TableDrop.java
package com.stackoverflow.test.Derby;
import java.sql.SQLException;
import java.sql.Statement;
public class TableDrop
{
public static void main(String args[]) throws ClassNotFoundException, SQLException
{
Connect.connectMe();
Statement st = Connect.con.createStatement();
String query = "Drop table myJavaTable";
boolean val = st.execute(query);
if(val==false) {
System.out.println("Table Dropped!");
} else {
System.out.println("Table cannot be dropped!");
}
Connect.con.close();
}
}
希望这些可以帮到你。如果您需要更多信息,请评论答案。如果您能为此做出贡献,请更新我的答案,以便对其他人更有用。