MacOS下JAVA用JDBC连接MySQL一直报错

frankpacino 发布于 2014/11/28 08:18
阅读 2K+
收藏 0
运行数据库连接测试程序,


控制台一直出现一句话:
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;

}


}

加载中
0
klaus_
klaus_
你这明明是测试用例有问题啊,不是mysql的问题吧。建议把代码贴出来看下
0
Lukewxl
Lukewxl
直接用JDBC连接mysql数据库,看看到底是什么问题
frankpacino
frankpacino
代码已贴
0
Lukewxl
Lukewxl

org.gjt.mm.mysql.Driver

哥们,这不是mysql的官方Driver吧,用mave或者去mysql官网下个正确的驱动吧


0
Barret_Guo
Barret_Guo
哎   哥们去百度一下在JDBC 怎么写  。。。不要动不动就说机器的问题 。
frankpacino
frankpacino
哥们,解决了。代码是老师给的,本身没错。是eclipse默认的启动配置错了
返回顶部
顶部