黑马程序员技术交流社区
标题:
使用JDBC-ODBC 方式如何连接MySQL数据库?
[打印本页]
作者:
wang90ling
时间:
2014-11-3 22:49
标题:
使用JDBC-ODBC 方式如何连接MySQL数据库?
使用JDBC-ODBC 方式如何连接MySQL数据库
作者:
feng_0207
时间:
2014-11-3 23:15
public static void main(String[] args) {
Connection conn = null;
Statement st = null;
try {
// 1.注册驱动
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName);
// DriverManager.registerDriver(new Driver());
// 2.创建连接
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "passw0rd";
conn = DriverManager.getConnection(url, username,
password);
// 3.获得Statement
st = conn.createStatement();
// 4.执行SQL
String sql = "insert into user (usernane, age) " +
"values ('cc', 22)";
//String sql2 = "update user set username='zsdsdf' " +
// "where username='cc'";
int n = st.executeUpdate(sql);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
// 6.关闭资源
st.close();
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
复制代码
以前的代码 供你参考。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2