日期:2014-05-20 浏览次数:20879 次
public class TestConnect {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://127.0.0.1:3306;DatabaseName = db1";
/**/
private static String user = "root";
private static String password = "jinsheng";
public static Connection getConnection(){
Connection con;
try{
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
System.out.println("success");
return con;
}catch(ClassNotFoundException e){
System.out.println(e.getMessage());
System.out.println("ClassNotFound");
}catch(SQLException e){
System.out.println(e.getMessage());
System.out.println("SQLNotFound");
}
return null;
}
//*******************************************************
public static void main(String [] args){
TestConnect con1 = new TestConnect();
//Connection connect = con1.getConnection();
Connection connect = TestConnect.getConnection();
//Chengyu chy = con1.getChy(connect);
/*if (chy!=null){
System.out.println("id = " + chy.getId());
System.out.println("headno = " + chy.getHeadno());
System.out.println("endno = " + chy.getEndno());
System.out.println("word = " + chy.getWord());
}*/
}
//***********************************************************/
}
------解决方案--------------------
参考这个看看,一个是学习笔记,一个是连接SQL Server的代码
http://www.java2000.net/viewthread.jsp?tid=4596
http://www.java2000.net/viewthread.jsp?tid=171
其它数据库的链接,可以到精华区找一个文章