I am trying to connect MySL database but some error in this code so please help me.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class MyClass {
static publ开发者_JS百科ic final String driver = "com.mysql.jdbc.Driver";
static public final String connection ="jdbc:mysql://localhost:3306/ashu";
static public final String user = "root";
static public final String pass= "admin";
ResultSet rs;
Connection con;
Statement smt;
public Connection openConnection(){
try{
Class.forName(driver);
con=DriverManager.getConnection(connection,user,pass);
smt=con.createStatement(rs.TYPE_SCROLL_SENSITIVE,rs.CONCUR_UPDATABLE);
}catch(Exception ex){
System.out.println("Connection Failed: "+ex);
}
return con;
}
public void closeConnection(){
try{
con.close();
}catch(Exception ex){
System.out.println("Closing Connection Failed*****");
}
}
public String getData(String id){
String result="";
openConnection();
try{
rs=smt.executeQuery("select name from mytable where no="+id);
while(rs.next()){
result=rs.getString(1);
}
}catch(Exception ex){
System.out.println("getData() Failed*****");
}
System.out.println("Result*****"+result);
closeConnection();
return result;
}
}
If you are targetting android phones, which seems you do, you better use SQLite.
See this tutorial if you want to create your own database at run time. Or you can follow this if you have your own database existed and you want to use it in your app.
Good Luck !
精彩评论