日期:2014-05-20  浏览次数:20700 次

数据库连接小问题
我是学.NET的,但公司让我转JAVA
我手头一点基础的资料都没有,上网找的资料都很复杂
谁可以给我些段最简单的JAVA连接数据库的代码
简单的就好,我自己研究这个过程,谢谢

------解决方案--------------------
这里有个最简单的实例,可以参考一下:
Java code
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

其它数据库的链接,可以到精华区找一个文章