MacOS下JAVA用JDBC连接MySQL一直报错
控制台一直出现一句话:
Usage: TestRunner [-wait] testCaseName, where name is the name of the TestCase class
找不到错误,程序无法运行。
希望大家指教
以下是代码:
package Database;
import java.sql.*;
public class JDBC_Test {
private static Connection connection;
private static Statement statement;
private static PreparedStatement prestatment;
public static void main(String[] args)
{
//connection = getConnection();
//insert();
//query();
delete();
query();
//update();
//query();
}
public static void insert()
{
connection = getConnection();
try{
String sql = "insert into departments(dept_no,dept_name) values ('d011','Test')";
statement = (Statement)connection.createStatement();
int count = statement.executeUpdate(sql);//execute statement
System.out.println("Insert " + count +" records into departments table");
connection.close();
}catch(SQLException e){
System.out.println("insert record failed" + e.getMessage());
}
}
public static void query() {
connection = getConnection(); //connect to database
try {
String sql = "select * from departments order by dept_no"; // select statement
statement = (Statement) connection.createStatement();
ResultSet rs = statement.executeQuery(sql); //execute statement
System.out.println("Query results are:");
while (rs.next()) { // not the last row
String department_no = rs.getString("dept_no");
String department_name = rs.getString("dept_name");
System.out.println(department_no + " " + department_name);
}
System.out.println("Use PreStatement:");
prestatment = connection.prepareStatement("select * from departments where dept_no = ?");
prestatment.setString(1, "d008");
ResultSet rs1 = prestatment.executeQuery();
while (rs1.next()) { // not the last row
String department_no = rs1.getString("dept_no");
String department_name = rs1.getString("dept_name");
System.out.println(department_no + " " + department_name);
}
connection.close();
} catch (SQLException e) {
System.out.println("Query failed" + e.getMessage());
}
}
public static void delete() {
connection = getConnection();
try {
String sql = "delete from departments where dept_no = 'd009'";
statement = (Statement) connection.createStatement();
int count = statement.executeUpdate(sql);
System.out.println("delete " + count + " record(s) from departments");
connection.close();
} catch (SQLException e) {
System.out.println("Delete record failed" + e.getMessage());
}
}
public static void update() {
connection = getConnection();
try {
String sql = "update departments set dept_name ='New_Lab' where dept_no = 'd010'";
statement = (Statement) connection.createStatement();
int count = statement.executeUpdate(sql);
System.out.println("update " + count + " records in departments");
connection.close();
} catch (SQLException e) {
System.out.println("Update failed " + e.getMessage());
}
}
public static Connection getConnection()
{
Connection c = null;
String ur1="jdbc:mysql://localhost:3306/test1?characterEncoding=utf8";
String username="root";
String password="root";
try{
Class.forName("org.gjt.mm.mysql.Driver");
c = DriverManager.getConnection(ur1,username,password);
System.out.println("Connect Success");
}
catch(ClassNotFoundException cnfex){
System.out.println("Failed to load JDBC driver.");
cnfex.printStackTrace();
System.exit(1);
}catch(SQLException sqlex){
System.err.println("Unable to connect");
sqlex.printStackTrace();
}
return c;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
哥们,解决了。代码是老师给的,本身没错。是eclipse默认的启动配置错了
哎 哥们去百度一下在JDBC 怎么写 。。。不要动不动就说机器的问题 。
org.gjt.mm.mysql.Driver
哥们,这不是mysql的官方Driver吧,用mave或者去mysql官网下个正确的驱动吧
代码已贴
直接用JDBC连接mysql数据库,看看到底是什么问题
你这明明是测试用例有问题啊,不是mysql的问题吧。建议把代码贴出来看下