本例子源自慕课网jsp,servlet生成报表课程的例子,使用的是mysql数据库
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcConn {
private static String url="jdbc:mysql://localhost:3336/testReport";
private static String username="root";
private static String password="myoa888";
private static Connection conn;
private static PreparedStatement ps;
private static Statement st;
private static ResultSet rs;
public static Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}