弱弱问java和mysql的连接问题
package testpro;
import java.sql.*;
public class TestDB2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestDB2.login();
}
public static void login(){
try{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
String url = "jdbc:odbc:mysqlobdc ";
//String username = "root ";
//String password = "123456 ";
Connection conn=DriverManager.getConnection(url, "root ", "123456 ");
Statement stat=conn.createStatement();
String sql= "SELECT*FROM lucky ";
ResultSet rs=stat.executeQuery(sql);
while(rs.next()){
String uname =rs.getString( "name ");
String id=rs.getString( "id ");
System.out.println(uname+ " "+id);
}
conn.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
------解决方案--------------------直接去mysql官网下driver,不要用odbc了
------解决方案--------------------String url = "jdbc:odbc:mysqlobdc ";没写IP端口和数据库名吧...
------解决方案--------------------大哥...URL是这么写的..
jdbc:mysql://IP地址/数据库名?useUnicode=true&characterEncoding=gb2312
useUnicode -> 使用UNICODE编码访问
characterEncoding -> 字符编码格式
------解决方案--------------------习惯用jdbc
------解决方案--------------------习惯JDBC