JDBC连接MySQL
加载及注册JDBC驱动程序
Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.jdbc.Driver").newInstance();
常见参数:
user 用户名
password 密码
建立连接对象
String sql="jdbc:mysql://localhost:3306/sample_db?user=root&password=your_password";
Connection con = DriverManager.getConnection(sql);
建立SQL陈述式对象(Statement Object)
Statement stmt = con.createStatement();
执行SQL语句
executeQuery()
String query = "select * from test";
ResultSet rs=stmt.executeQuery(query);
结果集ResultSet
while(rs.next())
{rs.getString(1);rs.getInt(2);}
executeUpdate()
String upd="insert into test (id,name) values(1001,xuzhaori)";
int con=stmt.executeUpdate(upd);
示例:
try
{
}
catch(SQLException sqle)
{
}
finally
{
}
标签:jdbc,String,rs,MySQL,JDBC,mysql,连接 From: https://www.cnblogs.com/yitongtianxia666/p/16830551.html