java连接mysql数据库(JDBC驱动) mysqljdbcjava
程序员文章站
2024-03-20 16:08:28
...
//需将java——JDBC相关jar包导入
//mysql-connector-java-版本-bin-jar
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class DataManage
{
public static void main(String[] argv)
{
System.out.println("-------- MySQL JDBC Connection Testing ------------");
try
{
// 之所以要使用下面这条语句,是因为要使用MySQL的驱动,所以我们要把它驱动起来,
// 可以通过Class.forName把它加载进去,也可以通过初始化来驱动起来,下面三种形式都可以
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;
String sql;
// MySQL的JDBC URL编写方式:jdbc:mysql://主机名称:连接端口/数据库的名称?参数=值
// 避免中文乱码要指定useUnicode和characterEncoding
String url = "jdbc:mysql://172.16.34.116:3306/TBL_AutoDate?"
+ "user=front&password=front&useUnicode=true&characterEncoding=UTF8";
//String url = ("jdbc:mysql://172.16.34.116:3306/TBL_AutoDate","front", "front");
try
{
connection = DriverManager.getConnection(url);
if (connection != null)
{
System.out.println("You made it, take control your database now!");
}
else
{
System.out.println("Failed to make connection!");
}///connect successful!
////Do what you want to do of the databases!
int result;
sql = "create table student5(NO char(20),name varchar(20),primary key(NO))";
// // Statement里面带有很多方法,比如executeUpdate可以实现插入,更新和删除等
Statement stmt = connection.createStatement();
int result = stmt.executeUpdate(sql);// executeUpdate语句会返回一个受影响的行数,如果返回-1就没有成功
if (result != -1)
{
System.out.println("创建数据表成功");
sql = "insert into student5(NO,name) values('2012003','name1')";
result = stmt.executeUpdate(sql);
sql = "insert into student5(NO,name) values('2012004','name2')";
result = stmt.executeUpdate(sql);
sql = "select * from student5";
ResultSet rs = stmt.executeQuery(sql);// executeQuery会返回结果的集合,否则返回空值
System.out.println("学号\t姓名");
while (rs.next())
{
System.out
.println(rs.getString(1) + "\t" + rs.getString(2));// 入如果返回的是int类型可以用getInt()
}
}
}
catch (SQLException e) {
System.out.println("MySQL操作错误");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
// finally {
// connection.close();
// }
} //end main
}
//mysql-connector-java-版本-bin-jar
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class DataManage
{
public static void main(String[] argv)
{
System.out.println("-------- MySQL JDBC Connection Testing ------------");
try
{
// 之所以要使用下面这条语句,是因为要使用MySQL的驱动,所以我们要把它驱动起来,
// 可以通过Class.forName把它加载进去,也可以通过初始化来驱动起来,下面三种形式都可以
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;
String sql;
// MySQL的JDBC URL编写方式:jdbc:mysql://主机名称:连接端口/数据库的名称?参数=值
// 避免中文乱码要指定useUnicode和characterEncoding
String url = "jdbc:mysql://172.16.34.116:3306/TBL_AutoDate?"
+ "user=front&password=front&useUnicode=true&characterEncoding=UTF8";
//String url = ("jdbc:mysql://172.16.34.116:3306/TBL_AutoDate","front", "front");
try
{
connection = DriverManager.getConnection(url);
if (connection != null)
{
System.out.println("You made it, take control your database now!");
}
else
{
System.out.println("Failed to make connection!");
}///connect successful!
////Do what you want to do of the databases!
int result;
sql = "create table student5(NO char(20),name varchar(20),primary key(NO))";
// // Statement里面带有很多方法,比如executeUpdate可以实现插入,更新和删除等
Statement stmt = connection.createStatement();
int result = stmt.executeUpdate(sql);// executeUpdate语句会返回一个受影响的行数,如果返回-1就没有成功
if (result != -1)
{
System.out.println("创建数据表成功");
sql = "insert into student5(NO,name) values('2012003','name1')";
result = stmt.executeUpdate(sql);
sql = "insert into student5(NO,name) values('2012004','name2')";
result = stmt.executeUpdate(sql);
sql = "select * from student5";
ResultSet rs = stmt.executeQuery(sql);// executeQuery会返回结果的集合,否则返回空值
System.out.println("学号\t姓名");
while (rs.next())
{
System.out
.println(rs.getString(1) + "\t" + rs.getString(2));// 入如果返回的是int类型可以用getInt()
}
}
}
catch (SQLException e) {
System.out.println("MySQL操作错误");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
// finally {
// connection.close();
// }
} //end main
}