简体   繁体   English

使用Netbeans IDE将MySQL连接到Java

[英]Connect MySQL to Java using Netbeans IDE

i would like to ask how to connect Java program in MySQL, help me please! 我想问一下如何在MySQL中连接Java程序,请帮帮我! and can you give me a demo just to show the records on Database, thanks in advance! 可以,请给我一个演示只是为了显示数据库上的记录,谢谢!

the 127.0.0.1:3306 is depends what is the settings of your database make sure you add the MySQL JDBC Driver in your Libraries then try this code.. 127.0.0.1:3306取决于数据库的设置,请确保您在库中添加了MySQL JDBC Driver,然后尝试执行此代码。

try{
    Class.forName("com.mysql.jdbc.Driver"); // to set the driver
    Connection connect = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306", "<Database username>", "<Database password>"); // connect to database
    Statement st = connect.createStatement(); 
    Resultset rs = st.executeQuery( "SELECT * FROM <tablename>" );

    while( rs.next() ){
            System.out.print( rs.getString(1) );
    }
}
catch( Exception e ){
    System.out.print( e );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM