云萌主云应用官方论坛
标题:
JAVA mysql数据库连接
[打印本页]
作者:
lady-niuniu
时间:
2017-1-10 11:56
标题:
JAVA mysql数据库连接
来源:
http://www.open-open.com/code/view/1454633126620
package learn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class Example01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String URL="jdbc:mysql://127.0.0.1:3306/test1";
String DRIVER="com.mysql.jdbc.Driver";
String USERNAME="root";
String PASSWD="123456";
String sql;
Connection connection =null;
try{
Class.forName(DRIVER);
connection=DriverManager.getConnection(URL,USERNAME,PASSWD);
Statement stmt = connection.createStatement();
sql = "select * from t1";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
}catch(SQLException e){
System.out.println("test1");
e.printStackTrace();
}catch(ClassNotFoundException e){
System.out.println("test2");
e.printStackTrace();
}finally{
try{
connection.close();
}catch(SQLException e){
System.out.println("test3");
e.printStackTrace();
}
}
}
}
复制代码
欢迎光临 云萌主云应用官方论坛 (https://yunmengzhu.com/)
Powered by Discuz! X3.4