本文主要是介绍JDBC连接数据库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import java.sql.*;
public class Jdbc {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("连接数据库成功");
} catch (ClassNotFoundException e) {
System.out.println("连接失败");
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Connection connect= DriverManager.getConnection(
"jdbc:mysql://localhost:3306/asda","root","root");
System.out.println("连接成功2222");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from test02");
while(rs.next()) {
System.out.println(rs.getString("name"));
}
} catch (Exception e) {
System.out.println("get data erro!@!");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这篇关于JDBC连接数据库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!