首页 > 代码库 > java jdbc操作mysql
java jdbc操作mysql
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class test {
public static final String DBDRIVER = "com.mysql.jdbc.Driver";
// 定义MySQL数据库的连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/test" ; //和本机mysql中名为t的数据库取得连接
// MySQL数据库的连接用户名
public static final String DBUSER = "root" ;
// MySQL数据库的连接密码
public static final String DBPASS = "123456" ;
public static void main(String args[]){
PreparedStatement ps = null;
ResultSet rs = null;
Connection ct = null;
try {
//加载驱动
Class.forName(DBDRIVER);
//取得连接
ct = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
String sql = "select * from user_login where id=? ";
ps=ct.prepareStatement(sql);
ps.setString(1, "1");
rs=ps.executeQuery();
while(rs.next()){
String name = rs.getString("username");
System.out.println(name);
}
} catch (Exception e){
e.printStackTrace();
}
}
}