JDBC连接MySql
public static void main(String
[] args
) {
String Driver
="com.mysql.jdbc.Driver";
String url
= "jdbc:mysql://localhost:3306/text?useUnicode=true&characterEncoding=utf-8&useSSL=false";
String user
= "root";
String password
= "root";
Connection conn
= null
;
try {
Class
.forName(Driver
);
conn
= DriverManager
.getConnection(url
,user
,password
);
PreparedStatement ps
= conn
.prepareStatement("SELECT * FROM user WHERE age=?");
ps
.setString(1, "30");
ResultSet res
=ps
.executeQuery();
while (res
.next()){
String name
= res
.getString("name");
int age
= res
.getInt("age");
System
.out
.println("姓名:"+name
+"年龄:"+age
);
}
} catch (Exception e
) {
e
.printStackTrace();
}finally {
try {
conn
.close();
} catch (SQLException e
) {
e
.printStackTrace();
}
}
}
转载请注明原文地址: https://mac.8miu.com/read-502493.html