mysql - database connection in java with separated class -
i wrote java code connect mysql database in main method, , success , results. when separate database code in separated class have problem connection database nothing
this code:
package database; import java.sql.resultset; import java.sql.sqlexception; public class database { public static void main(string[] args) throws classnotfoundexception, sqlexception { //connection connect = drivermanager.getconnection("jdbc:mysql://localhost/javadb", "root", "123"); system.out.println("sadsa"); connecttodatabase db = new connecttodatabase("javadb", "root", "123"); system.out.println("sadsa"); string sqlstatement = "select *\n" + "from `javadb`.`user`\n" + " limit 1000;"; resultset result = db.makequery(sqlstatement); while (result.next()) { system.out.println(result.getstring("id") + " " + result.getstring("username") + " " + result.getstring("password") ); } db.closeconnection(); } }
the separated class:
package database; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; public class connecttodatabase { // database variables private final string databasename;// = "jdbc:mysql://localhost/javadb"; //url private final string databaseusername;// = "root"; private final string databasepassword;// = "123"; private connection connect; private statement sqlstatement; // connect database public connecttodatabase( string givendatabasename,string givendatabaseusername, string givendatabasepassword) throws sqlexception, classnotfoundexception { class.forname("com.mysql.jdbc.driver"); this.databasename = givendatabasename; this.databaseusername = givendatabaseusername; this.databasepassword = givendatabasepassword; this.connect = drivermanager.getconnection("jdbc:mysql://localhost/"+givendatabasename, givendatabasename, givendatabasepassword); this.sqlstatement = this.connect.createstatement(); } public resultset makequery(string givenstatement) throws sqlexception{ setsqlstatement(givenstatement); return sqlstatement.executequery(givenstatement); } public string getdatabaseusername() { return databaseusername; } public string getdatabasepassword() { return databasepassword; } public string getdatabasename() { return databasename; } public void setsqlstatement(statement sqlstatement) { this.sqlstatement = sqlstatement; } public void closeconnection() throws sqlexception{ connect.close(); } private void setsqlstatement(string givenstatement) { throw new unsupportedoperationexception("not supported yet."); //to change body of generated methods, choose tools | templates. } }
note: in main method if write same information connect directly work right:
connection connect = drivermanager.getconnection("jdbc:mysql://localhost/javadb", "root", "123");
this error message:
exception in thread "main" java.sql.sqlexception: access denied user 'javadb'@'localhost' (using password: yes) @ com.mysql.jdbc.sqlerror.createsqlexception(sqlerror.java:1074) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:4120) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:4052) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:925) @ com.mysql.jdbc.mysqlio.secureauth411(mysqlio.java:4615) @ com.mysql.jdbc.mysqlio.dohandshake(mysqlio.java:1302) @ com.mysql.jdbc.connectionimpl.coreconnect(connectionimpl.java:2465) @ com.mysql.jdbc.connectionimpl.connectonetryonly(connectionimpl.java:2498) @ com.mysql.jdbc.connectionimpl.createnewio(connectionimpl.java:2283) @ com.mysql.jdbc.connectionimpl.<init>(connectionimpl.java:822) @ com.mysql.jdbc.jdbc4connection.<init>(jdbc4connection.java:47) @ sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method) @ sun.reflect.nativeconstructoraccessorimpl.newinstance(unknown source) @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(unknown source) @ java.lang.reflect.constructor.newinstance(unknown source) @ com.mysql.jdbc.util.handlenewinstance(util.java:411) @ com.mysql.jdbc.connectionimpl.getinstance(connectionimpl.java:404) @ com.mysql.jdbc.nonregisteringdriver.connect(nonregisteringdriver.java:317) @ java.sql.drivermanager.getconnection(unknown source) @ java.sql.drivermanager.getconnection(unknown source) @ database.connecttodatabase.<init>(connecttodatabase.java:27) @ database.database.main(database.java:15) java result: 1
thank much
you not passing right parameter values when create connecttodatabase
instance. can show part of code?
Comments
Post a Comment