您的位置:首页 > 数据库

jdbc连接数据库

2016-05-27 20:21 423 查看
package testdb;

import java.sql.*;

public class TestDb {

public static void main(String[] args) {
String strDBDriver="com.mysql.jdbc.Driver";
   String strDBUrl="jdbc:mysql://localhost:3306/test";
   
   try {
Class.forName(strDBDriver).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
   
   Connection conn = null;
try {
conn = DriverManager.getConnection(strDBUrl,"root","root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//创建数据库连接对象 
   Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
   String sql="select count(*) as num from i_s_logon";
   try {
ResultSet  rs=stmt.executeQuery(sql);
while(rs.next()){
System.out.println("num="+rs.getInt("num"));
//System.out.println("password="+rs.getString("password"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
   
   

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jdbc 数据库